|
| 1 | +package containertools_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/operator-framework/operator-registry/pkg/containertools" |
| 12 | + "github.com/operator-framework/operator-registry/pkg/containertools/containertoolsfakes" |
| 13 | + |
| 14 | + "github.com/sirupsen/logrus" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + expectedFilePath = "testdata/expected_unpack" |
| 20 | +) |
| 21 | + |
| 22 | +func TestReadImageLayersDocker(t *testing.T) { |
| 23 | + image := "quay.io/operator-framework/example" |
| 24 | + testWorkingDir := "testdata/docker" |
| 25 | + testOutputDir := "testdata/output" |
| 26 | + |
| 27 | + expectedFiles, err := helperGetExpectedFiles() |
| 28 | + |
| 29 | + logger := logrus.NewEntry(logrus.New()) |
| 30 | + mockCmd := containertoolsfakes.FakeCommandRunner{} |
| 31 | + |
| 32 | + mockCmd.PullReturns(nil) |
| 33 | + mockCmd.SaveReturns(nil) |
| 34 | + |
| 35 | + imageReader := containertools.ImageLayerReader{ |
| 36 | + Cmd: &mockCmd, |
| 37 | + Logger: logger, |
| 38 | + } |
| 39 | + |
| 40 | + err = imageReader.GetImageData(image, testOutputDir, containertools.WithWorkingDir(testWorkingDir)) |
| 41 | + require.NoError(t, err) |
| 42 | + |
| 43 | + for _, file := range expectedFiles { |
| 44 | + expectedFilePath := filepath.Join(expectedFilePath, file) |
| 45 | + expectedFile, err := ioutil.ReadFile(expectedFilePath) |
| 46 | + require.NoError(t, err) |
| 47 | + |
| 48 | + actualFilePath := filepath.Join(testOutputDir, file) |
| 49 | + actualFile, err := ioutil.ReadFile(actualFilePath) |
| 50 | + require.NoError(t, err) |
| 51 | + |
| 52 | + require.Equal(t, string(expectedFile), string(actualFile)) |
| 53 | + } |
| 54 | + |
| 55 | + err = os.RemoveAll(testOutputDir) |
| 56 | + require.NoError(t, err) |
| 57 | +} |
| 58 | + |
| 59 | +func TestReadImageLayersPodman(t *testing.T) { |
| 60 | + image := "quay.io/operator-framework/example" |
| 61 | + testWorkingDir := "testdata/podman" |
| 62 | + testOutputDir := "testdata/output" |
| 63 | + |
| 64 | + expectedFiles, err := helperGetExpectedFiles() |
| 65 | + |
| 66 | + logger := logrus.NewEntry(logrus.New()) |
| 67 | + mockCmd := containertoolsfakes.FakeCommandRunner{} |
| 68 | + |
| 69 | + mockCmd.PullReturns(nil) |
| 70 | + mockCmd.SaveReturns(nil) |
| 71 | + |
| 72 | + imageReader := containertools.ImageLayerReader{ |
| 73 | + Cmd: &mockCmd, |
| 74 | + Logger: logger, |
| 75 | + } |
| 76 | + |
| 77 | + err = imageReader.GetImageData(image, testOutputDir, containertools.WithWorkingDir(testWorkingDir)) |
| 78 | + require.NoError(t, err) |
| 79 | + |
| 80 | + for _, file := range expectedFiles { |
| 81 | + expectedFilePath := filepath.Join(expectedFilePath, file) |
| 82 | + expectedFile, err := ioutil.ReadFile(expectedFilePath) |
| 83 | + require.NoError(t, err) |
| 84 | + |
| 85 | + actualFilePath := filepath.Join(testOutputDir, file) |
| 86 | + actualFile, err := ioutil.ReadFile(actualFilePath) |
| 87 | + require.NoError(t, err) |
| 88 | + |
| 89 | + require.Equal(t, string(expectedFile), string(actualFile)) |
| 90 | + } |
| 91 | + |
| 92 | + err = os.RemoveAll(testOutputDir) |
| 93 | + require.NoError(t, err) |
| 94 | +} |
| 95 | + |
| 96 | +func TestReadImageLayers_PullError(t *testing.T) { |
| 97 | + image := "quay.io/operator-framework/example" |
| 98 | + testOutputDir := "testdata/output" |
| 99 | + |
| 100 | + logger := logrus.NewEntry(logrus.New()) |
| 101 | + mockCmd := containertoolsfakes.FakeCommandRunner{} |
| 102 | + |
| 103 | + mockCmd.PullReturns(fmt.Errorf("Unable to pull image")) |
| 104 | + |
| 105 | + imageReader := containertools.ImageLayerReader{ |
| 106 | + Cmd: &mockCmd, |
| 107 | + Logger: logger, |
| 108 | + } |
| 109 | + |
| 110 | + err := imageReader.GetImageData(image, testOutputDir) |
| 111 | + require.Error(t, err) |
| 112 | +} |
| 113 | + |
| 114 | +func TestReadImageLayers_SaveError(t *testing.T) { |
| 115 | + image := "quay.io/operator-framework/example" |
| 116 | + testOutputDir := "testdata/output" |
| 117 | + |
| 118 | + logger := logrus.NewEntry(logrus.New()) |
| 119 | + mockCmd := containertoolsfakes.FakeCommandRunner{} |
| 120 | + |
| 121 | + mockCmd.PullReturns(nil) |
| 122 | + mockCmd.SaveReturns(fmt.Errorf("Unable to save image")) |
| 123 | + |
| 124 | + imageReader := containertools.ImageLayerReader{ |
| 125 | + Cmd: &mockCmd, |
| 126 | + Logger: logger, |
| 127 | + } |
| 128 | + |
| 129 | + err := imageReader.GetImageData(image, testOutputDir) |
| 130 | + require.Error(t, err) |
| 131 | +} |
| 132 | + |
| 133 | +func helperGetExpectedFiles() ([]string, error) { |
| 134 | + var files []string |
| 135 | + |
| 136 | + err := filepath.Walk(expectedFilePath, func(path string, f os.FileInfo, err error) error { |
| 137 | + if !f.IsDir() { |
| 138 | + fileName := strings.Replace(path, expectedFilePath, "", -1) |
| 139 | + files = append(files, fileName) |
| 140 | + } |
| 141 | + return nil |
| 142 | + }) |
| 143 | + |
| 144 | + return files, err |
| 145 | +} |
0 commit comments