|
| 1 | +// +build integration |
| 2 | + |
| 3 | +package tests |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "fmt" |
| 8 | + "io/ioutil" |
| 9 | + "os/exec" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + diffBase = "gcr.io/gcp-runtimes/diff-base" |
| 17 | + diffModified = "gcr.io/gcp-runtimes/diff-modified" |
| 18 | + |
| 19 | + aptBase = "gcr.io/gcp-runtimes/apt-base" |
| 20 | + aptModified = "gcr.io/gcp-runtimes/apt-modified" |
| 21 | + |
| 22 | + // Why is this node-modified:2.0? |
| 23 | + nodeBase = "gcr.io/gcp-runtimes/node-modified:2.0" |
| 24 | + nodeModified = "gcr.io/gcp-runtimes/node-modified" |
| 25 | + |
| 26 | + pipModified = "gcr.io/gcp-runtimes/pip-modified" |
| 27 | + |
| 28 | + multiBase = "gcr.io/gcp-runtimes/multi-base" |
| 29 | + multiModified = "gcr.io/gcp-runtimes/multi-modified" |
| 30 | +) |
| 31 | + |
| 32 | +type ContainerDiffRunner struct { |
| 33 | + t *testing.T |
| 34 | + binaryPath string |
| 35 | +} |
| 36 | + |
| 37 | +func (c *ContainerDiffRunner) Run(command ...string) (string, error) { |
| 38 | + path, err := filepath.Abs(c.binaryPath) |
| 39 | + if err != nil { |
| 40 | + c.t.Fatalf("Error finding container-diff binary: %s", err) |
| 41 | + } |
| 42 | + c.t.Logf("Running command: %s %s", path, command) |
| 43 | + cmd := exec.Command(path, command...) |
| 44 | + |
| 45 | + var stdout, stderr bytes.Buffer |
| 46 | + cmd.Stdout = &stdout |
| 47 | + cmd.Stderr = &stderr |
| 48 | + if err := cmd.Run(); err != nil { |
| 49 | + return "", fmt.Errorf("Error running command %s: %s Stderr: %s", command, err, stderr.String()) |
| 50 | + } |
| 51 | + return stdout.String(), nil |
| 52 | +} |
| 53 | + |
| 54 | +func TestDiffAndAnalysis(t *testing.T) { |
| 55 | + runner := ContainerDiffRunner{ |
| 56 | + t: t, |
| 57 | + binaryPath: "../out/container-diff", |
| 58 | + } |
| 59 | + |
| 60 | + var tests = []struct { |
| 61 | + description string |
| 62 | + imageA string |
| 63 | + imageB string |
| 64 | + differFlag string |
| 65 | + subcommand string |
| 66 | + |
| 67 | + //TODO: Don't consume a json file |
| 68 | + expectedFile string |
| 69 | + }{ |
| 70 | + { |
| 71 | + description: "file differ", |
| 72 | + subcommand: "diff", |
| 73 | + imageA: diffBase, |
| 74 | + imageB: diffModified, |
| 75 | + differFlag: "-f", |
| 76 | + expectedFile: "file_diff_expected.json", |
| 77 | + }, |
| 78 | + { |
| 79 | + description: "apt differ", |
| 80 | + subcommand: "diff", |
| 81 | + imageA: aptBase, |
| 82 | + imageB: aptModified, |
| 83 | + differFlag: "-a", |
| 84 | + expectedFile: "apt_diff_expected.json", |
| 85 | + }, |
| 86 | + { |
| 87 | + description: "node differ", |
| 88 | + subcommand: "diff", |
| 89 | + imageA: nodeBase, |
| 90 | + imageB: nodeModified, |
| 91 | + differFlag: "-n", |
| 92 | + expectedFile: "node_diff_order_expected.json", |
| 93 | + }, |
| 94 | + { |
| 95 | + description: "multi differ", |
| 96 | + subcommand: "diff", |
| 97 | + imageA: multiBase, |
| 98 | + imageB: multiModified, |
| 99 | + differFlag: "-npa", |
| 100 | + expectedFile: "multi_diff_expected.json", |
| 101 | + }, |
| 102 | + { |
| 103 | + description: "history differ", |
| 104 | + subcommand: "diff", |
| 105 | + imageA: diffBase, |
| 106 | + imageB: diffModified, |
| 107 | + differFlag: "-d", |
| 108 | + expectedFile: "hist_diff_expected.json", |
| 109 | + }, |
| 110 | + { |
| 111 | + description: "apt sorted differ", |
| 112 | + subcommand: "diff", |
| 113 | + imageA: aptBase, |
| 114 | + imageB: aptModified, |
| 115 | + differFlag: "-ao", |
| 116 | + expectedFile: "apt_sorted_diff_expected.json", |
| 117 | + }, |
| 118 | + { |
| 119 | + description: "apt analysis", |
| 120 | + subcommand: "analyze", |
| 121 | + imageA: aptModified, |
| 122 | + differFlag: "-a", |
| 123 | + expectedFile: "apt_analysis_expected.json", |
| 124 | + }, |
| 125 | + { |
| 126 | + description: "file sorted analysis", |
| 127 | + subcommand: "analyze", |
| 128 | + imageA: diffModified, |
| 129 | + differFlag: "-fo", |
| 130 | + expectedFile: "file_sorted_analysis_expected.json", |
| 131 | + }, |
| 132 | + { |
| 133 | + description: "pip analysis", |
| 134 | + subcommand: "analyze", |
| 135 | + imageA: pipModified, |
| 136 | + differFlag: "-p", |
| 137 | + expectedFile: "pip_analysis_expected.json", |
| 138 | + }, |
| 139 | + { |
| 140 | + description: "node analysis", |
| 141 | + subcommand: "analyze", |
| 142 | + imageA: nodeModified, |
| 143 | + differFlag: "-n", |
| 144 | + expectedFile: "node_analysis_expected.json", |
| 145 | + }, |
| 146 | + } |
| 147 | + for _, test := range tests { |
| 148 | + t.Run(test.description, func(t *testing.T) { |
| 149 | + args := []string{test.subcommand, test.imageA} |
| 150 | + if test.imageB != "" { |
| 151 | + args = append(args, test.imageB) |
| 152 | + } |
| 153 | + args = append(args, test.differFlag) |
| 154 | + args = append(args, "-j") |
| 155 | + actual, err := runner.Run(args...) |
| 156 | + if err != nil { |
| 157 | + t.Fatalf("Error running command: %s", err) |
| 158 | + } |
| 159 | + e, err := ioutil.ReadFile(test.expectedFile) |
| 160 | + if err != nil { |
| 161 | + t.Fatalf("Error reading expected file output file: %s", err) |
| 162 | + } |
| 163 | + actual = strings.TrimSpace(actual) |
| 164 | + expected := strings.TrimSpace(string(e)) |
| 165 | + if actual != expected { |
| 166 | + t.Errorf("Error actual output does not match expected. \n\nExpected: %s\n\n Actual: %s", expected, actual) |
| 167 | + } |
| 168 | + }) |
| 169 | + } |
| 170 | +} |
0 commit comments