github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/test/e2e/23_data_injection_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // SPDX-FileCopyrightText: 2021-Present The Jackal Authors 3 4 // Package test provides e2e tests for Jackal. 5 package test 6 7 import ( 8 "context" 9 "fmt" 10 "net/http" 11 "path/filepath" 12 "testing" 13 "time" 14 15 "github.com/Racer159/jackal/src/pkg/cluster" 16 "github.com/Racer159/jackal/src/pkg/utils/exec" 17 "github.com/stretchr/testify/require" 18 ) 19 20 func TestDataInjection(t *testing.T) { 21 t.Log("E2E: Data injection") 22 e2e.SetupWithCluster(t) 23 24 path := fmt.Sprintf("build/jackal-package-kiwix-%s-3.5.0.tar", e2e.Arch) 25 26 tmpdir := t.TempDir() 27 sbomPath := filepath.Join(tmpdir, ".sbom-location") 28 29 // Repeat the injection action 3 times to ensure the data injection is idempotent and doesn't fail to perform an upgrade 30 for i := 0; i < 3; i++ { 31 runDataInjection(t, path) 32 } 33 34 // Verify the file and injection marker were created 35 runningKiwixPod, _, err := e2e.Kubectl("--namespace=kiwix", "get", "pods", "--selector=app=kiwix-serve", "--field-selector=status.phase=Running", "--output=jsonpath={.items[0].metadata.name}") 36 require.NoError(t, err) 37 stdOut, stdErr, err := e2e.Kubectl("--namespace=kiwix", "logs", runningKiwixPod, "--tail=5", "-c=kiwix-serve") 38 require.NoError(t, err, stdOut, stdErr) 39 require.Contains(t, stdOut, "devops.stackexchange.com_en_all_2023-05.zim") 40 require.Contains(t, stdOut, ".jackal-injection-") 41 42 // need target to equal svc that we are trying to connect to call checkForJackalConnectLabel 43 c, err := cluster.NewCluster() 44 require.NoError(t, err) 45 tunnel, err := c.Connect("kiwix") 46 require.NoError(t, err) 47 defer tunnel.Close() 48 49 // Ensure connection 50 resp, err := http.Get(tunnel.HTTPEndpoint()) 51 require.NoError(t, err, resp) 52 require.Equal(t, 200, resp.StatusCode) 53 54 // Remove the data injection example 55 stdOut, stdErr, err = e2e.Jackal("package", "remove", path, "--confirm") 56 require.NoError(t, err, stdOut, stdErr) 57 58 // Ensure that the `requirements.txt` file is discovered correctly 59 stdOut, stdErr, err = e2e.Jackal("package", "inspect", path, "--sbom-out", sbomPath) 60 require.NoError(t, err, stdOut, stdErr) 61 require.FileExists(t, filepath.Join(sbomPath, "kiwix", "compare.html"), "A compare.html file should have been made") 62 63 require.FileExists(t, filepath.Join(sbomPath, "kiwix", "sbom-viewer-jackal-component-kiwix-serve.html"), "The data-injection component should have an SBOM viewer") 64 require.FileExists(t, filepath.Join(sbomPath, "kiwix", "jackal-component-kiwix-serve.json"), "The data-injection component should have an SBOM json") 65 } 66 67 func runDataInjection(t *testing.T, path string) { 68 // Limit this deploy to 5 minutes 69 ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Minute) 70 defer cancel() 71 72 // Deploy the data injection example 73 stdOut, stdErr, err := exec.CmdWithContext(ctx, exec.PrintCfg(), e2e.JackalBinPath, "package", "deploy", path, "-l", "trace", "--confirm") 74 require.NoError(t, err, stdOut, stdErr) 75 }