github.com/replicatedhq/ship@v0.55.0/integration/unfork/integration_test.go (about)

     1  package base
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"os"
     9  	"path"
    10  	"path/filepath"
    11  	"testing"
    12  
    13  	"github.com/docker/docker/client"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	"github.com/onsi/gomega/format"
    17  	"github.com/replicatedhq/ship/integration"
    18  	"github.com/replicatedhq/ship/pkg/cli"
    19  	"github.com/replicatedhq/ship/pkg/state"
    20  	yaml "gopkg.in/yaml.v3"
    21  )
    22  
    23  type TestMetadata struct {
    24  	Upstream     string              `yaml:"upstream"`
    25  	Fork         string              `yaml:"fork"`
    26  	Args         []string            `yaml:"args"`
    27  	MakeAbsolute bool                `yaml:"make_absolute"`
    28  	IgnoredKeys  map[string][]string `yaml:"ignoredKeys"`
    29  	IgnoredFiles []string            `yaml:"ignoredFiles"`
    30  }
    31  
    32  func TestUnfork(t *testing.T) {
    33  	RegisterFailHandler(Fail)
    34  	format.MaxDepth = 30
    35  	RunSpecs(t, "ship unfork")
    36  }
    37  
    38  var _ = Describe("ship unfork", func() {
    39  	dockerClient, err := client.NewClientWithOpts(client.FromEnv)
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  	dockerClient.NegotiateAPIVersion(context.Background())
    44  
    45  	integrationDir, err := os.Getwd()
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  
    50  	files, err := ioutil.ReadDir(integrationDir)
    51  	if err != nil {
    52  		panic(err)
    53  	}
    54  
    55  	for _, file := range files {
    56  		if file.IsDir() {
    57  			When(fmt.Sprintf("the spec in %q is run", file.Name()), func() {
    58  				testPath := path.Join(integrationDir, file.Name())
    59  				var testOutputPath string
    60  				var testMetadata TestMetadata
    61  
    62  				BeforeEach(func() {
    63  					err = os.Setenv("NO_OS_EXIT", "1")
    64  					Expect(err).NotTo(HaveOccurred())
    65  					// create a temporary directory within this directory to compare files with
    66  					testOutputPath, err = ioutil.TempDir(testPath, "_test_")
    67  					Expect(err).NotTo(HaveOccurred())
    68  					err = os.Chdir(testOutputPath)
    69  					Expect(err).NotTo(HaveOccurred())
    70  
    71  					// read the test metadata
    72  					testMetadata = readMetadata(testPath)
    73  
    74  				}, 20)
    75  
    76  				AfterEach(func() {
    77  					err := state.GetSingleton().RemoveStateFile()
    78  					Expect(err).NotTo(HaveOccurred())
    79  					err = os.Chdir(integrationDir)
    80  					Expect(err).NotTo(HaveOccurred())
    81  				}, 20)
    82  
    83  				It("Should output the expected files", func() {
    84  					replacements := map[string]string{}
    85  					absoluteUpstream := testMetadata.Upstream
    86  
    87  					if testMetadata.MakeAbsolute {
    88  						relativePath := testMetadata.Upstream
    89  						pwdRoot, err := os.Getwd()
    90  						Expect(err).NotTo(HaveOccurred())
    91  						pwdRoot, err = filepath.Abs(pwdRoot)
    92  						Expect(err).NotTo(HaveOccurred())
    93  						absolutePath := filepath.Join(pwdRoot, "..")
    94  						absoluteUpstream = fmt.Sprintf("file::%s", filepath.Join(absolutePath, relativePath))
    95  						replacements["__upstream__"] = absoluteUpstream
    96  					}
    97  
    98  					cmd := cli.RootCmd()
    99  					buf := new(bytes.Buffer)
   100  					cmd.SetOutput(buf)
   101  					cmd.SetArgs(append([]string{
   102  						"unfork",
   103  						"--upstream",
   104  						absoluteUpstream,
   105  						testMetadata.Fork,
   106  						"--log-level=off",
   107  					}, testMetadata.Args...))
   108  					err := cmd.Execute()
   109  					Expect(err).NotTo(HaveOccurred())
   110  
   111  					if testMetadata.IgnoredKeys == nil {
   112  						testMetadata.IgnoredKeys = make(map[string][]string)
   113  					}
   114  					if _, ok := testMetadata.IgnoredKeys[".ship/state.json"]; ok { //nolint:gosimple
   115  						testMetadata.IgnoredKeys[".ship/state.json"] = append(testMetadata.IgnoredKeys[".ship/state.json"], "v1.shipVersion")
   116  					} else {
   117  						testMetadata.IgnoredKeys[".ship/state.json"] = []string{"v1.shipVersion"}
   118  					}
   119  
   120  					// compare the files in the temporary directory with those in the "expected" directory
   121  					result, err := integration.CompareDir(path.Join(testPath, "expected"), testOutputPath, replacements, testMetadata.IgnoredFiles, testMetadata.IgnoredKeys)
   122  					Expect(err).NotTo(HaveOccurred())
   123  					Expect(result).To(BeTrue())
   124  
   125  					// run 'ship watch' and expect no error to occur
   126  					watchCmd := cli.RootCmd()
   127  					watchBuf := new(bytes.Buffer)
   128  					watchCmd.SetOutput(watchBuf)
   129  					watchCmd.SetArgs(append([]string{"watch", "--exit"}, testMetadata.Args...))
   130  					err = watchCmd.Execute()
   131  					Expect(err).NotTo(HaveOccurred())
   132  				}, 60)
   133  			})
   134  		}
   135  	}
   136  })
   137  
   138  func readMetadata(testPath string) TestMetadata {
   139  	var testMetadata TestMetadata
   140  	metadataBytes, err := ioutil.ReadFile(path.Join(testPath, "metadata.yaml"))
   141  	Expect(err).NotTo(HaveOccurred())
   142  	err = yaml.Unmarshal(metadataBytes, &testMetadata)
   143  	Expect(err).NotTo(HaveOccurred())
   144  
   145  	return testMetadata
   146  }