Skip to content

Commit

Permalink
test: add e2e test regarding export cache (okteto#3270)
Browse files Browse the repository at this point in the history
* test: add e2e test regarding export cache

Signed-off-by: Javier López Barba <javier@okteto.com>

* fix: use whitespaces instead of tabs

Signed-off-by: Javier López Barba <javier@okteto.com>

* fix: sprintf args

Signed-off-by: Javier López Barba <javier@okteto.com>

* fix: key name

Signed-off-by: Javier López Barba <javier@okteto.com>

* use a different name

Signed-off-by: Javier López Barba <javier@okteto.com>

* fix: containerport

Signed-off-by: Javier López Barba <javier@okteto.com>

* fix: test

Signed-off-by: Javier López Barba <javier@okteto.com>

* fix: e2e tests

Signed-off-by: Javier López Barba <javier@okteto.com>

* fix: namespace image

Signed-off-by: Javier López Barba <javier@okteto.com>

Signed-off-by: Javier López Barba <javier@okteto.com>
  • Loading branch information
jLopezbarb committed Dec 12, 2022
1 parent 98f2709 commit 5ee67ae
Showing 1 changed file with 137 additions and 0 deletions.
137 changes: 137 additions & 0 deletions integration/deploy/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,70 @@ var (
context: app
deploy:
- kubectl apply -f k8s.yml
`
oktetoManifestContentWithCache = `build:
app:
context: app
export_cache: okteto.dev/app:dev
cache_from: okteto.dev/app:dev
image: okteto.dev/app:dev
deploy:
- kubectl apply -f k8s.yml
`
oktetoManifestWithDestroyContent = `build:
app:
context: app
deploy:
- okteto destroy
- kubectl apply -f k8s.yml
`

appDockerfileWithCache = `FROM python:alpine
EXPOSE 2931
RUN --mount=type=cache,target=/root/.cache echo hola`
k8sManifestTemplateWithCache = `
apiVersion: apps/v1
kind: Deployment
metadata:
name: e2etest
spec:
replicas: 1
selector:
matchLabels:
app: e2etest
template:
metadata:
labels:
app: e2etest
spec:
terminationGracePeriodSeconds: 1
containers:
- name: test
image: %s
ports:
- containerPort: 8080
workingDir: /usr/src/app
env:
- name: VAR
value: value1
command:
- sh
- -c
- "echo -n $VAR > var.html && python -m http.server 8080"
---
apiVersion: v1
kind: Service
metadata:
name: e2etest
annotations:
dev.okteto.com/auto-ingress: "true"
spec:
type: ClusterIP
ports:
- name: e2etest
port: 8080
selector:
app: e2etest
`
)

Expand Down Expand Up @@ -240,6 +297,53 @@ func TestDeployOktetoManifestWithDestroy(t *testing.T) {
require.True(t, k8sErrors.IsNotFound(err))
}

// TestDeployOktetoManifestExportCache tests the following scenario:
// - Deploying a okteto manifest locally with a build that has a export cache
func TestDeployOktetoManifestExportCache(t *testing.T) {
t.Parallel()
oktetoPath, err := integration.GetOktetoPath()
require.NoError(t, err)

dir := t.TempDir()

testNamespace := integration.GetTestNamespace("TestDeployExportCache", user)
namespaceOpts := &commands.NamespaceOptions{
Namespace: testNamespace,
OktetoHome: dir,
Token: token,
}
require.NoError(t, commands.RunOktetoCreateNamespace(oktetoPath, namespaceOpts))
defer commands.RunOktetoDeleteNamespace(oktetoPath, namespaceOpts)
require.NoError(t, commands.RunOktetoKubeconfig(oktetoPath, dir))
c, _, err := okteto.NewK8sClientProvider().Provide(kubeconfig.Get([]string{filepath.Join(dir, ".kube", "config")}))
require.NoError(t, err)

require.NoError(t, createOktetoManifestWithCache(dir))
require.NoError(t, createAppDockerfileWithCache(dir))
require.NoError(t, createK8sManifestWithCache(dir, testNamespace))

deployOptions := &commands.DeployOptions{
Workdir: dir,
Namespace: testNamespace,
OktetoHome: dir,
Token: token,
}
require.NoError(t, commands.RunOktetoDeploy(oktetoPath, deployOptions))

// Test that image has been built
require.NotEmpty(t, getImageWithSHA(fmt.Sprintf("%s/%s/app:dev", okteto.Context().Registry, testNamespace)))

destroyOptions := &commands.DestroyOptions{
Workdir: dir,
Namespace: testNamespace,
OktetoHome: dir,
}
require.NoError(t, commands.RunOktetoDestroy(oktetoPath, destroyOptions))

_, err = integration.GetService(context.Background(), testNamespace, "e2etest", c)
require.True(t, k8sErrors.IsNotFound(err))
}

func isImageBuilt(image string) bool {
reg := registry.NewOktetoRegistry()
if _, err := reg.GetImageTagWithDigest(image); err == nil {
Expand All @@ -248,6 +352,15 @@ func isImageBuilt(image string) bool {
return false
}

func createOktetoManifestWithCache(dir string) error {
dockerfilePath := filepath.Join(dir, oktetoManifestName)
dockerfileContent := []byte(oktetoManifestContentWithCache)
if err := os.WriteFile(dockerfilePath, dockerfileContent, 0600); err != nil {
return err
}
return nil
}

func createOktetoManifest(dir string) error {
dockerfilePath := filepath.Join(dir, oktetoManifestName)
dockerfileContent := []byte(oktetoManifestContent)
Expand All @@ -271,3 +384,27 @@ func expectForceBuild(output string) error {
}
return nil
}

func createK8sManifestWithCache(dir, ns string) error {
dockerfilePath := filepath.Join(dir, k8sManifestName)
appImageDev := fmt.Sprintf("%s/%s/app:dev", okteto.Context().Registry, ns)

dockerfileContent := []byte(fmt.Sprintf(k8sManifestTemplateWithCache, appImageDev))
if err := os.WriteFile(dockerfilePath, dockerfileContent, 0600); err != nil {
return err
}
return nil
}

func createAppDockerfileWithCache(dir string) error {
if err := os.Mkdir(filepath.Join(dir, "app"), 0700); err != nil {
return err
}

appDockerfilePath := filepath.Join(dir, "app", "Dockerfile")
appDockerfileContent := []byte(appDockerfileWithCache)
if err := os.WriteFile(appDockerfilePath, appDockerfileContent, 0600); err != nil {
return err
}
return nil
}

0 comments on commit 5ee67ae

Please sign in to comment.