diff --git a/.env b/.env new file mode 100644 index 0000000..a0ff0aa --- /dev/null +++ b/.env @@ -0,0 +1,3 @@ +KNUU_TIMEOUT=60m # The timeout for the tests +KNUU_BUILDER=docker # The builder to use for building images. docker or kubernetes +LOG_LEVEL=info # The debug level. debug, info, warn, error diff --git a/README.md b/README.md index 97db024..cf1e536 100644 --- a/README.md +++ b/README.md @@ -186,7 +186,7 @@ You can set the following environment variables to change the behavior of knuu: | --- | --- | --- | --- | | `KNUU_TIMEOUT` | The timeout for the tests. | Any valid duration | `60m` | | `KNUU_BUILDER` | The builder to use for building images. | `docker`, `kubernetes` | `docker` | -| `DEBUG_LEVEL` | The debug level. | `debug`, `info`, `warn`, `error` | `info` | +| `LOG_LEVEL` | The debug level. | `debug`, `info`, `warn`, `error` | `info` | --- diff --git a/e2e/basic/.env b/e2e/basic/.env new file mode 100644 index 0000000..053fa0c --- /dev/null +++ b/e2e/basic/.env @@ -0,0 +1,3 @@ +TEST=test +TEST_2=test2 +TEST_3=test3 diff --git a/.gitignore b/e2e/basic/.gitignore similarity index 100% rename from .gitignore rename to e2e/basic/.gitignore diff --git a/e2e/basic/env_to_json_test.go b/e2e/basic/env_to_json_test.go new file mode 100644 index 0000000..89cbe66 --- /dev/null +++ b/e2e/basic/env_to_json_test.go @@ -0,0 +1,128 @@ +package basic + +import ( + "encoding/json" + "fmt" + "os" + "testing" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" + + "github.com/celestiaorg/knuu/pkg/knuu" +) + +func TestEnvToJSON(t *testing.T) { + t.Parallel() + // Setup + + executor, err := knuu.NewExecutor() + if err != nil { + t.Fatalf("Error creating executor: %v", err) + } + + const numberOfInstances = 2 + instances := make([]*knuu.Instance, numberOfInstances) + + // get the values from the .env + + test := os.Getenv("TEST") + test2 := os.Getenv("TEST_2") + test3 := os.Getenv("TEST_3") + jsonBytes, err := json.Marshal(map[string]string{ + "TEST": test, + "TEST_2": test2, + "TEST_3": test3, + }) + if err != nil { + t.Fatalf("Error converting env vars to JSON: %v", err) + } + jsonString := string(jsonBytes) + logrus.Debugf("JSON: %v", jsonString) + + for i := 0; i < numberOfInstances; i++ { + instanceName := fmt.Sprintf("web%d", i+1) + instance, err := knuu.NewInstance(instanceName) + if err != nil { + t.Fatalf("Error creating instance '%v': %v", instanceName, err) + } + err = instance.SetImage("docker.io/nginx:latest") + if err != nil { + t.Fatalf("Error setting image for '%v': %v", instanceName, err) + } + instance.AddPortTCP(80) + _, err = instance.ExecuteCommand("mkdir", "-p", "/usr/share/nginx/html") + if err != nil { + t.Fatalf("Error executing command for '%v': %v", instanceName, err) + } + + logrus.Debugf("Writing JSON to instance '%v': %v", instanceName, jsonString) + _, err = instance.ExecuteCommand("mkdir", "-p", "/opt/env") + if err != nil { + t.Fatalf("Error writing JSON to instance '%v': %v", instanceName, err) + } + // write the json file to the instance + _, err = instance.ExecuteCommand("echo", fmt.Sprintf("'%s'", jsonString), ">", "/opt/env/env.json") + if err != nil { + t.Fatalf("Error writing JSON to instance '%v': %v", instanceName, err) + } + // for testing it, we also add it as index.html to the nginx server + _, err = instance.ExecuteCommand("echo", fmt.Sprintf("'%s'", jsonString), ">", "/usr/share/nginx/html/index.html") + if err != nil { + t.Fatalf("Error writing JSON to instance '%v': %v", instanceName, err) + } + + err = instance.Commit() + if err != nil { + t.Fatalf("Error committing instance '%v': %v", instanceName, err) + } + + instances[i] = instance + } + + t.Cleanup(func() { + // Cleanup + if os.Getenv("KNUU_SKIP_CLEANUP") != "true" { + err := executor.Destroy() + if err != nil { + t.Fatalf("Error destroying executor: %v", err) + } + + for _, instance := range instances { + if instance != nil { + err := instance.Destroy() + if err != nil { + t.Fatalf("Error destroying instance: %v", err) + } + } + } + } + }) + + // Test logic + for _, instance := range instances { + err = instance.StartAsync() + if err != nil { + t.Fatalf("Error waiting for instance to be running: %v", err) + } + } + + for _, instance := range instances { + webIP, err := instance.GetIP() + if err != nil { + t.Fatalf("Error getting IP: %v", err) + } + + err = instance.WaitInstanceIsRunning() + if err != nil { + t.Fatalf("Error waiting for instance to be running: %v", err) + } + + wget, err := executor.ExecuteCommand("wget", "-q", "-O", "-", webIP) + if err != nil { + t.Fatalf("Error executing command: %v", err) + } + + assert.Equal(t, "{\"TEST\":\"test\",\"TEST_2\":\"test2\",\"TEST_3\":\"test3\"}\n", wget) + } +} diff --git a/go.mod b/go.mod index 4b6386c..b9d388e 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/cosmos/cosmos-sdk v0.46.16 github.com/docker/docker v26.1.0+incompatible github.com/google/uuid v1.6.0 + github.com/joho/godotenv v1.5.1 github.com/minio/minio-go/v7 v7.0.69 github.com/rs/zerolog v1.32.0 github.com/sirupsen/logrus v1.9.3 diff --git a/go.sum b/go.sum index 2743688..1e05fe1 100644 --- a/go.sum +++ b/go.sum @@ -1307,6 +1307,8 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGw github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/jmhodges/levigo v1.0.0 h1:q5EC36kV79HWeTBWsod3mG11EgStG3qArTKcvlksN1U= github.com/jmhodges/levigo v1.0.0/go.mod h1:Q6Qx+uH3RAqyK4rFQroq9RL7mdkABMcfhEI+nNuzMJQ= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= diff --git a/pkg/knuu/errors.go b/pkg/knuu/errors.go index 8daf52e..12fbc45 100644 --- a/pkg/knuu/errors.go +++ b/pkg/knuu/errors.go @@ -211,4 +211,5 @@ var ( ErrCannotStartInstance = &Error{Code: "Cannot Start Instance", Message: "cannot start instance"} ErrMinioNotInitialized = &Error{Code: "MinioNotInitialized", Message: "minio not initialized"} ErrGeneratingK8sNameForPreloader = &Error{Code: "GeneratingK8sNameForPreloader", Message: "error generating k8s name for preloader"} + ErrCannotLoadEnv = &Error{Code: "Cannot Load Env", Message: "cannot load env"} ) diff --git a/pkg/knuu/knuu.go b/pkg/knuu/knuu.go index 90d4b63..7c503f0 100644 --- a/pkg/knuu/knuu.go +++ b/pkg/knuu/knuu.go @@ -13,6 +13,7 @@ import ( "syscall" "time" + "github.com/joho/godotenv" "github.com/sirupsen/logrus" rbacv1 "k8s.io/api/rbac/v1" @@ -49,6 +50,12 @@ func Scope() string { // InitializeWithScope initializes knuu with a given scope func InitializeWithScope(scope string) error { + var err error + err = godotenv.Load() + if err != nil { + return ErrCannotLoadEnv.Wrap(err) + } + if scope == "" { return ErrCannotInitializeKnuuWithEmptyScope } @@ -84,7 +91,6 @@ func InitializeWithScope(scope string) error { ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() - var err error k8sClient, err = k8s.New(ctx, scope) if err != nil { return ErrCannotInitializeK8s.Wrap(err)