github.com/containerd/nerdctl@v1.7.7/pkg/testutil/compose.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package testutil
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  )
    24  
    25  type ComposeDir struct {
    26  	t            testing.TB
    27  	dir          string
    28  	yamlBasePath string
    29  }
    30  
    31  func (cd *ComposeDir) WriteFile(name, content string) {
    32  	if err := os.WriteFile(filepath.Join(cd.dir, name), []byte(content), 0644); err != nil {
    33  		cd.t.Fatal(err)
    34  	}
    35  }
    36  
    37  func (cd *ComposeDir) YAMLFullPath() string {
    38  	return filepath.Join(cd.dir, cd.yamlBasePath)
    39  }
    40  
    41  func (cd *ComposeDir) Dir() string {
    42  	return cd.dir
    43  }
    44  
    45  func (cd *ComposeDir) ProjectName() string {
    46  	return filepath.Base(cd.dir)
    47  }
    48  
    49  func (cd *ComposeDir) CleanUp() {
    50  	os.RemoveAll(cd.dir)
    51  }
    52  
    53  func NewComposeDir(t testing.TB, dockerComposeYAML string) *ComposeDir {
    54  	tmpDir, err := os.MkdirTemp("", "nerdctl-compose-test")
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	cd := &ComposeDir{
    59  		t:            t,
    60  		dir:          tmpDir,
    61  		yamlBasePath: "docker-compose.yaml",
    62  	}
    63  	cd.WriteFile(cd.yamlBasePath, dockerComposeYAML)
    64  	return cd
    65  }