|
| 1 | +package framework |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + . "github.com/onsi/ginkgo" |
| 13 | + . "github.com/onsi/gomega" |
| 14 | + . "github.com/onsi/gomega/gbytes" |
| 15 | + . "github.com/onsi/gomega/gexec" |
| 16 | + "github.com/sirupsen/logrus" |
| 17 | +) |
| 18 | + |
| 19 | +// TestFramework is used to support commonly used test features |
| 20 | +type TestFramework struct { |
| 21 | + crioDir string |
| 22 | +} |
| 23 | + |
| 24 | +// NewTestFramework creates a new test framework instance |
| 25 | +func NewTestFramework() *TestFramework { |
| 26 | + return &TestFramework{""} |
| 27 | +} |
| 28 | + |
| 29 | +// Setup is the global initialization function which runs before each test |
| 30 | +// suite |
| 31 | +func (t *TestFramework) Setup(dir string) { |
| 32 | + // Global initialization for the whole framework goes in here |
| 33 | + logrus.SetLevel(logrus.DebugLevel) |
| 34 | + logrus.SetOutput(GinkgoWriter) |
| 35 | + t.crioDir = dir |
| 36 | +} |
| 37 | + |
| 38 | +// Teardown is the global deinitialization function which runs after each test |
| 39 | +// suite |
| 40 | +func (t *TestFramework) Teardown() { |
| 41 | +} |
| 42 | + |
| 43 | +// Describe is a convenience wrapper around the `ginkgo.Describe` function |
| 44 | +func (t *TestFramework) Describe(text string, body func()) bool { |
| 45 | + return Describe("crictl: "+text, body) |
| 46 | +} |
| 47 | + |
| 48 | +// Convenience method for command creation |
| 49 | +func cmd(workDir, format string, args ...interface{}) *Session { |
| 50 | + c := strings.Split(fmt.Sprintf(format, args...), " ") |
| 51 | + command := exec.Command(c[0], c[1:]...) |
| 52 | + if workDir != "" { |
| 53 | + command.Dir = workDir |
| 54 | + } |
| 55 | + |
| 56 | + session, err := Start(command, GinkgoWriter, GinkgoWriter) |
| 57 | + Expect(err).To(BeNil()) |
| 58 | + |
| 59 | + return session |
| 60 | +} |
| 61 | + |
| 62 | +// Convenience method for command creation in the current working directory |
| 63 | +func lcmd(format string, args ...interface{}) *Session { |
| 64 | + return cmd("", format, args...) |
| 65 | +} |
| 66 | + |
| 67 | +// Run crictl and return the resulting session |
| 68 | +func (t *TestFramework) Crictl(args string) *Session { |
| 69 | + return lcmd("crictl %s", args).Wait() |
| 70 | +} |
| 71 | + |
| 72 | +// Run crictl on the specified endpoint and return the resulting session |
| 73 | +func (t *TestFramework) CrictlWithEndpoint(endpoint, args string) *Session { |
| 74 | + return lcmd("crictl --runtime-endpoint=%s %s", endpoint, args).Wait() |
| 75 | +} |
| 76 | + |
| 77 | +// Run crictl and expect success containing the specified output |
| 78 | +func (t *TestFramework) CrictlExpectSuccess(args, expectedOut string) { |
| 79 | + t.CrictlExpectSuccessWithEndpoint("", args, expectedOut) |
| 80 | +} |
| 81 | + |
| 82 | +// Run crictl and expect success containing the specified output |
| 83 | +func (t *TestFramework) CrictlExpectSuccessWithEndpoint(endpoint, args, expectedOut string) { |
| 84 | + // When |
| 85 | + res := t.CrictlWithEndpoint(endpoint, args) |
| 86 | + |
| 87 | + // Then |
| 88 | + Expect(res).To(Exit(0)) |
| 89 | + Expect(res.Out).To(Say(expectedOut)) |
| 90 | + Expect(res.Err.Contents()).To(BeEmpty()) |
| 91 | +} |
| 92 | + |
| 93 | +// Run crictl and expect error containing the specified outputs |
| 94 | +func (t *TestFramework) CrictlExpectFailure( |
| 95 | + args string, expectedOut, expectedErr string, |
| 96 | +) { |
| 97 | + // When |
| 98 | + res := t.Crictl(args) |
| 99 | + |
| 100 | + // Then |
| 101 | + Expect(res).To(Exit(1)) |
| 102 | + Expect(res.Out).To(Say(expectedOut)) |
| 103 | + Expect(res.Err).To(Say(expectedErr)) |
| 104 | +} |
| 105 | + |
| 106 | +func SetupCrio() string { |
| 107 | + const ( |
| 108 | + crioURL = "https://github.com/cri-o/cri-o" |
| 109 | + timeout = 10 * time.Minute |
| 110 | + ) |
| 111 | + tmpDir := filepath.Join(os.TempDir(), "crio-tmp") |
| 112 | + |
| 113 | + if _, err := os.Stat(tmpDir); os.IsNotExist(err) { |
| 114 | + logrus.Info("cloning and building CRI-O") |
| 115 | + lcmd("git clone --depth=1 %s %s", crioURL, tmpDir).Wait(timeout) |
| 116 | + cmd(tmpDir, "make").Wait(timeout) |
| 117 | + } |
| 118 | + |
| 119 | + return tmpDir |
| 120 | +} |
| 121 | + |
| 122 | +// Start the container runtime process |
| 123 | +func (t *TestFramework) StartCrio() (string, string, *Session) { |
| 124 | + // Create a new sandbox directory |
| 125 | + tmpDir, err := ioutil.TempDir("", "crictl-e2e-") |
| 126 | + Expect(err).To(BeNil()) |
| 127 | + |
| 128 | + // Copy everything together |
| 129 | + lcmd("cp -R %s %s", filepath.Join(t.crioDir, "bin"), tmpDir).Wait() |
| 130 | + |
| 131 | + lcmd("cp %s %s", filepath.Join(t.crioDir, "test", "policy.json"), |
| 132 | + tmpDir).Wait() |
| 133 | + |
| 134 | + lcmd("cp %s %s", filepath.Join(t.crioDir, "contrib", "cni", |
| 135 | + "10-crio-bridge.conf"), tmpDir).Wait() |
| 136 | + |
| 137 | + for _, d := range []string{ |
| 138 | + "cni-config", "root", "runroot", "log", "exits", "attach", |
| 139 | + } { |
| 140 | + Expect(os.MkdirAll(filepath.Join(tmpDir, d), 0755)).To(BeNil()) |
| 141 | + } |
| 142 | + |
| 143 | + endpoint := filepath.Join(tmpDir, "crio.sock") |
| 144 | + |
| 145 | + session := cmd(tmpDir, "%s"+ |
| 146 | + " --listen=%s"+ |
| 147 | + " --conmon=%s"+ |
| 148 | + " --container-exits-dir=%s"+ |
| 149 | + " --container-attach-socket-dir=%s"+ |
| 150 | + " --log-dir=%s"+ |
| 151 | + " --signature-policy=%s"+ |
| 152 | + " --cni-config-dir=%s"+ |
| 153 | + " --root=%s"+ |
| 154 | + " --runroot=%s"+ |
| 155 | + " --storage-driver=vfs", |
| 156 | + filepath.Join(tmpDir, "bin", "crio"), |
| 157 | + endpoint, |
| 158 | + filepath.Join(tmpDir, "bin", "conmon"), |
| 159 | + filepath.Join(tmpDir, "exits"), |
| 160 | + filepath.Join(tmpDir, "attach"), |
| 161 | + filepath.Join(tmpDir, "log"), |
| 162 | + filepath.Join(tmpDir, "policy.json"), |
| 163 | + filepath.Join(tmpDir, "cni-config"), |
| 164 | + filepath.Join(tmpDir, "root"), |
| 165 | + filepath.Join(tmpDir, "runroot"), |
| 166 | + ) |
| 167 | + |
| 168 | + // Wait for the connection to be available |
| 169 | + for i := 0; i < 100; i++ { |
| 170 | + res := t.CrictlWithEndpoint(endpoint, "--timeout=200ms info") |
| 171 | + if res.ExitCode() == 0 { |
| 172 | + break |
| 173 | + } |
| 174 | + logrus.Info("waiting for CRI-O to become ready") |
| 175 | + } |
| 176 | + return endpoint, tmpDir, session |
| 177 | +} |
| 178 | + |
| 179 | +// Stop the container runtime process |
| 180 | +func (t *TestFramework) StopCrio(testDir string, session *Session) { |
| 181 | + Expect(session.Interrupt().Wait()).To(Exit(0)) |
| 182 | + Expect(os.RemoveAll(testDir)).To(BeNil()) |
| 183 | +} |
0 commit comments