Skip to content

Commit

Permalink
split e2e test functions since gh actions borked
Browse files Browse the repository at this point in the history
  • Loading branch information
hellt committed Mar 29, 2023
1 parent 60396f8 commit 4bf0fe6
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 93 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ jobs:
test:
[
"TestSrlinuxReconciler_BareSrlinuxCR",
"TestSrlinuxReconciler_WithStartupConfig",
"TestSrlinuxReconciler_WithJSONStartupConfig",
"TestSrlinuxReconciler_WithCLIStartupConfig",
]

steps:
Expand Down
186 changes: 94 additions & 92 deletions tests/e2e/srlinux_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,131 +183,133 @@ func TestSrlinuxReconciler_BareSrlinuxCR(t *testing.T) {
})
}

// TestSrlinuxReconciler_WithStartupConfig tests the reconciliation of the Srlinux custom resources
// which are provided with the startup config in JSON and CLI formats.
func TestSrlinuxReconciler_WithStartupConfig(t *testing.T) {
// TestSrlinuxReconciler_WithJSONStartupConfig tests the reconciliation of the Srlinux custom resource
// provided with the JSON-styled startup config.
func TestSrlinuxReconciler_WithJSONStartupConfig(t *testing.T) {
namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: SrlinuxNamespace,
},
}

srl1Name := "srl1"
srl2Name := "srl2"
srlName := "srl1"

srl1NsName := types.NamespacedName{Name: srl1Name, Namespace: SrlinuxNamespace}
srl2NsName := types.NamespacedName{Name: srl2Name, Namespace: SrlinuxNamespace}
srlNsNames := []types.NamespacedName{srl1NsName, srl2NsName}
srlNsName := types.NamespacedName{Name: srlName, Namespace: SrlinuxNamespace}

setup := func(t *testing.T, g *WithT) {
t.Helper()

createNamespace(t, g, namespace)
createConfigMapFromFile(t, g, srl1Name+"-config", "config.json", "./configs/test.json")
createConfigMapFromFile(t, g, srl2Name+"-config", "config.cli", "./configs/test.cli")
createConfigMapFromFile(t, g, srlName+"-config", "config.json", "./configs/test.json")
}

t.Run("Should reconcile a Srlinux custom resource", func(t *testing.T) {
g := NewWithT(t)
testReconciliationWithConfig(t, setup, srlNsName, "config.json")
})
}

setup(t, g)
// defer deleteNamespace(t, g, namespace)
// TestSrlinuxReconciler_WithJSONStartupConfig tests the reconciliation of the Srlinux custom resource
// provided with the CLI-styled startup config.
func TestSrlinuxReconciler_WithCLIStartupConfig(t *testing.T) {
namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: SrlinuxNamespace,
},
}

t.Log("Checking that Srlinux resources do not exist in the cluster")
srl1 := &srlinuxv1.Srlinux{}
srl2 := &srlinuxv1.Srlinux{}
srlName := "srl1"

err := k8sClient.Get(ctx, srl1NsName, srl1)
g.Expect(errors.IsNotFound(err)).To(BeTrue())
srlNsName := types.NamespacedName{Name: srlName, Namespace: SrlinuxNamespace}

err = k8sClient.Get(ctx, srl2NsName, srl2)
g.Expect(errors.IsNotFound(err)).To(BeTrue())
setup := func(t *testing.T, g *WithT) {
t.Helper()

t.Log("Creating the custom resources with startup config present")
srl1 = &srlinuxv1.Srlinux{
ObjectMeta: metav1.ObjectMeta{
Name: srl1Name,
Namespace: SrlinuxNamespace,
},
TypeMeta: srlTypeMeta,
Spec: srlinuxv1.SrlinuxSpec{
Config: &srlinuxv1.NodeConfig{
Image: testImageName,
ConfigDataPresent: true,
ConfigFile: "config.json",
},
},
}
g.Expect(k8sClient.Create(ctx, srl1)).Should(Succeed())
createNamespace(t, g, namespace)
createConfigMapFromFile(t, g, srlName+"-config", "config.cli", "./configs/test.cli")
}

srl2 = &srlinuxv1.Srlinux{
ObjectMeta: metav1.ObjectMeta{
Name: srl2Name,
Namespace: SrlinuxNamespace,
},
TypeMeta: srlTypeMeta,
Spec: srlinuxv1.SrlinuxSpec{
Config: &srlinuxv1.NodeConfig{
Image: testImageName,
ConfigDataPresent: true,
ConfigFile: "config.cli",
},
t.Run("Should reconcile a Srlinux custom resource", func(t *testing.T) {
testReconciliationWithConfig(t, setup, srlNsName, "config.cli")
})
}

func testReconciliationWithConfig(
t *testing.T,
setup func(t *testing.T, g *WithT),
srlNsName types.NamespacedName,
configFile string,
) {
g := NewWithT(t)

setup(t, g)

t.Log("Checking that Srlinux resources do not exist in the cluster")

srl := &srlinuxv1.Srlinux{}

err := k8sClient.Get(ctx, srlNsName, srl)
g.Expect(errors.IsNotFound(err)).To(BeTrue())

t.Log("Creating the custom resources with startup config present")

srl = &srlinuxv1.Srlinux{
ObjectMeta: metav1.ObjectMeta{
Name: srlNsName.Name,
Namespace: SrlinuxNamespace,
},
TypeMeta: srlTypeMeta,
Spec: srlinuxv1.SrlinuxSpec{
Config: &srlinuxv1.NodeConfig{
Image: testImageName,
ConfigDataPresent: true,
ConfigFile: configFile,
},
}
g.Expect(k8sClient.Create(ctx, srl2)).Should(Succeed())
},
}
g.Expect(k8sClient.Create(ctx, srl)).Should(Succeed())

t.Log("Checking if the custom resources were successfully created")
for _, srlNsName := range srlNsNames {
g.Eventually(func() error {
found := &srlinuxv1.Srlinux{}
t.Log("Checking if the custom resources were successfully created")

return k8sClient.Get(ctx, srlNsName, found)
}).Should(Succeed())
}
g.Eventually(func() error {
found := &srlinuxv1.Srlinux{}

// Reconcile is triggered by the creation of the custom resource
return k8sClient.Get(ctx, srlNsName, found)
}).Should(Succeed())

t.Log("Checking if Srlinux Pods were successfully created in the reconciliation")
for _, srlNsName := range srlNsNames {
g.Eventually(func() error {
found := &corev1.Pod{}
// Reconcile is triggered by the creation of the custom resource

return k8sClient.Get(ctx, srlNsName, found)
}).Should(Succeed())
}
t.Log("Checking if Srlinux Pods were successfully created in the reconciliation")
g.Eventually(func() error {
found := &corev1.Pod{}

t.Log("Ensuring the Srlinux CR Ready status reached true")
for _, srlNsName := range srlNsNames {
g.Eventually(func() bool {
srl := &srlinuxv1.Srlinux{}
g.Expect(k8sClient.Get(ctx, srlNsName, srl)).Should(Succeed())
return k8sClient.Get(ctx, srlNsName, found)
}).Should(Succeed())

return srl.Status.Ready == true
}, srlinuxMaxReadyTime).Should(BeTrue())
}
t.Log("Ensuring the Srlinux CR Ready status reached true")
g.Eventually(func() bool {
srl := &srlinuxv1.Srlinux{}
g.Expect(k8sClient.Get(ctx, srlNsName, srl)).Should(Succeed())

t.Log("Ensuring Srlinux config state is loaded")
for _, srlNsName := range srlNsNames {
// reuse max ready time, which should be more than enought to apply config
g.Eventually(func() bool {
srl := &srlinuxv1.Srlinux{}
g.Expect(k8sClient.Get(ctx, srlNsName, srl)).Should(Succeed())
return srl.Status.Ready == true
}, srlinuxMaxReadyTime).Should(BeTrue())

return srl.Status.StartupConfig.Phase == "loaded"
}, srlinuxMaxReadyTime).Should(BeTrue())
}
t.Log("Ensuring Srlinux config state is loaded")
// reuse max ready time, which should be more than enought to apply config
g.Eventually(func() bool {
srl := &srlinuxv1.Srlinux{}
g.Expect(k8sClient.Get(ctx, srlNsName, srl)).Should(Succeed())

t.Log("Ensuring Srlinux config state is applied")
for _, srlNsName := range srlNsNames {
//nolint:gosec
cmd := exec.Command("kubectl", "exec", "-n", SrlinuxNamespace,
srlNsName.Name, "--", "sr_cli", "info", "from", "state", "interface", "mgmt0", "description")
return srl.Status.StartupConfig.Phase == "loaded"
}, srlinuxMaxReadyTime).Should(BeTrue())

b, err := cmd.CombinedOutput()
t.Log("Ensuring Srlinux config state is applied")
//nolint:gosec
cmd := exec.Command("kubectl", "exec", "-n", SrlinuxNamespace,
srlNsName.Name, "--", "sr_cli", "info", "from", "state", "interface", "mgmt0", "description")

g.Expect(err).ShouldNot(HaveOccurred())
b, err := cmd.CombinedOutput()

g.Expect(string(b)).Should(ContainSubstring("set from e2e test"))
}
})
g.Expect(err).ShouldNot(HaveOccurred())

g.Expect(string(b)).Should(ContainSubstring("set from e2e test"))
}

0 comments on commit 4bf0fe6

Please sign in to comment.