|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/go-check/check" |
| 12 | + "gotest.tools/icmd" |
| 13 | + "sigs.k8s.io/kind/pkg/cluster" |
| 14 | + "sigs.k8s.io/kind/pkg/cluster/config/encoding" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + KubectlTraceBinary = os.Getenv("TEST_KUBECTLTRACE_BINARY") |
| 19 | + KindImageTag = os.Getenv("TEST_KIND_IMAGETAG") |
| 20 | +) |
| 21 | + |
| 22 | +type KubectlTraceSuite struct { |
| 23 | + kubeConfigPath string |
| 24 | + kindContext *cluster.Context |
| 25 | +} |
| 26 | + |
| 27 | +func init() { |
| 28 | + if KubectlTraceBinary == "" { |
| 29 | + KubectlTraceBinary = "kubectl-trace" |
| 30 | + } |
| 31 | + |
| 32 | + if KindImageTag == "" { |
| 33 | + KindImageTag = "kindest/node:v1.12.3" |
| 34 | + } |
| 35 | + check.Suite(&KubectlTraceSuite{}) |
| 36 | +} |
| 37 | + |
| 38 | +func (k *KubectlTraceSuite) SetUpSuite(c *check.C) { |
| 39 | + cfg, err := encoding.Load("") |
| 40 | + c.Assert(err, check.IsNil) |
| 41 | + retain := false |
| 42 | + wait := time.Duration(0) |
| 43 | + |
| 44 | + err = cfg.Validate() |
| 45 | + c.Assert(err, check.IsNil) |
| 46 | + |
| 47 | + clusterName, err := generateClusterName() |
| 48 | + c.Assert(err, check.IsNil) |
| 49 | + ctx := cluster.NewContext(clusterName) |
| 50 | + err = ctx.Create(cfg, retain, wait) |
| 51 | + c.Assert(err, check.IsNil) |
| 52 | + k.kindContext = ctx |
| 53 | +} |
| 54 | + |
| 55 | +func (s *KubectlTraceSuite) TearDownSuite(c *check.C) { |
| 56 | + err := s.kindContext.Delete() |
| 57 | + c.Assert(err, check.IsNil) |
| 58 | +} |
| 59 | + |
| 60 | +func Test(t *testing.T) { check.TestingT(t) } |
| 61 | + |
| 62 | +func (k *KubectlTraceSuite) KubectlTraceCmd(c *check.C, args ...string) string { |
| 63 | + args = append([]string{fmt.Sprintf("--kubeconfig=%s", k.kindContext.KubeConfigPath())}, args...) |
| 64 | + res := icmd.RunCommand(KubectlTraceBinary, args...) |
| 65 | + c.Assert(res.ExitCode, check.Equals, icmd.Success.ExitCode) |
| 66 | + return res.Combined() |
| 67 | +} |
| 68 | + |
| 69 | +func generateClusterName() (string, error) { |
| 70 | + buf := make([]byte, 10) |
| 71 | + if _, err := rand.Read(buf); err != nil { |
| 72 | + return "", err |
| 73 | + } |
| 74 | + return strings.ToLower(fmt.Sprintf("%X", buf)), nil |
| 75 | +} |
0 commit comments