Skip to content

Commit

Permalink
store the okteto and syncthing logs as artifacts in the CI job (#923)
Browse files Browse the repository at this point in the history
* reset the kubernetes client to avoid keeping state between tests
* add more logs to the integration tests
* store the okteto and syncthing logs as artifacts
* add an env var to skip deleting folders (for CI)

Signed-off-by: Ramiro Berrelleza <rberrelleza@gmail.com>
  • Loading branch information
rberrelleza committed Jun 16, 2020
1 parent bd2d1b9 commit 4396101
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 36 deletions.
25 changes: 16 additions & 9 deletions .circleci/config.yml
Expand Up @@ -59,14 +59,16 @@ jobs:
/usr/local/bin/okteto login --token ${API_TOKEN}
- run:
name: integration tests (serverside)
command: |
unset OKTETO_CLIENTSIDE_TRANSLATION
make integration
environment:
OKTETO_CLIENTSIDE_TRANSLATION: ''
OKTETO_SKIP_CLEANUP: 'true'
command: make integration
- run:
name: integration tests (clientside)
command: |
export OKTETO_CLIENTSIDE_TRANSLATION=true
make integration
environment:
OKTETO_SKIP_CLEANUP: 'true'
OKTETO_CLIENTSIDE_TRANSLATION: 'true'
command: make integration
- save_cache:
key: v3-pkg-cache-{{ checksum "go.sum" }}
paths:
Expand All @@ -92,12 +94,12 @@ jobs:
executor: win/default
steps:
- checkout
- restore_cache:
keys:
- v4-pkg-cache-windows-{{ checksum "go.sum" }}
- run:
name: upgrade golang
command: choco upgrade golang
- restore_cache:
keys:
- v4-pkg-cache-windows-{{ checksum "go.sum" }}
- run:
name: test
command: |
Expand All @@ -118,13 +120,16 @@ jobs:
name: integration tests (serverside)
environment:
OKTETO_PATH: 'C:\Users\circleci\project\artifacts\bin\okteto.exe'
OKTETO_SKIP_CLEANUP: 'true'
OKTETO_CLIENTSIDE_TRANSLATION: ''
command: |
go test github.com/okteto/okteto/integration -tags=integration --count=1 -v
- run:
name: integration tests (clientside)
environment:
OKTETO_PATH: 'C:\Users\circleci\project\artifacts\bin\okteto.exe'
OKTETO_CLIENTSIDE_TRANSLATION: 'true'
OKTETO_SKIP_CLEANUP: 'true'
command: |
go test github.com/okteto/okteto/integration -tags=integration --count=1 -v
- save_cache:
Expand All @@ -133,6 +138,8 @@ jobs:
- C:\Users\circleci\AppData\Local\go-build
- C:\Users\circleci\go\pkg
- C:\Go\pkg
- store_artifacts:
path: C:\Users\circleci\.okteto
release:
docker:
- image: circleci/golang:1.14
Expand Down
9 changes: 7 additions & 2 deletions cmd/down.go
Expand Up @@ -15,6 +15,7 @@ package cmd

import (
"context"
"os"

"github.com/okteto/okteto/cmd/utils"
"github.com/okteto/okteto/pkg/analytics"
Expand Down Expand Up @@ -64,9 +65,13 @@ func Down() *cobra.Command {
return err
}
log.Success("Persistent volume removed")
if err := syncthing.RemoveFolder(dev); err != nil {
log.Infof("failed to delete existing syncthing folder")

if os.Getenv("OKTETO_SKIP_CLEANUP") == "" {
if err := syncthing.RemoveFolder(dev); err != nil {
log.Infof("failed to delete existing syncthing folder")
}
}

analytics.TrackDownVolumes(true)
}

Expand Down
65 changes: 49 additions & 16 deletions integration/integration_test.go
Expand Up @@ -24,6 +24,7 @@ import (
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
Expand All @@ -35,6 +36,7 @@ import (
"github.com/Masterminds/semver"
ps "github.com/mitchellh/go-ps"
okCmd "github.com/okteto/okteto/cmd"
"github.com/okteto/okteto/pkg/config"
k8Client "github.com/okteto/okteto/pkg/k8s/client"
"github.com/okteto/okteto/pkg/syncthing"
"go.undefinedlabs.com/scopeagent"
Expand Down Expand Up @@ -188,8 +190,9 @@ func TestDownloadSyncthing(t *testing.T) {

func TestAll(t *testing.T) {
ctx := scopeagent.GetContextFromTest(t)
tName := fmt.Sprintf("TestAll-%s-%s", runtime.GOOS, mode)
test := scopeagent.GetTest(t)
tName := fmt.Sprintf("TestAll-%s-%s", runtime.GOOS, mode)

test.Run(tName, func(t *testing.T) {
oktetoPath, err := getOktetoPath(ctx)
if err != nil {
Expand All @@ -200,6 +203,8 @@ func TestAll(t *testing.T) {
t.Fatalf("kubectl is not in the path: %s", err)
}

k8Client.Reset()

name := strings.ToLower(fmt.Sprintf("%s-%d", tName, time.Now().Unix()))
namespace := fmt.Sprintf("%s-%s", name, user)

Expand All @@ -215,7 +220,11 @@ func TestAll(t *testing.T) {
}

contentPath := filepath.Join(dir, "index.html")
ioutil.WriteFile(contentPath, []byte(name), 0644)
if err := ioutil.WriteFile(contentPath, []byte(name), 0644); err != nil {
t.Fatal(err)
}

log.Printf("original content: %s", name)

manifestPath := filepath.Join(dir, "okteto.yml")
if err := writeManifest(manifestPath, name); err != nil {
Expand Down Expand Up @@ -243,6 +252,7 @@ func TestAll(t *testing.T) {

log.Println("getting synchronized content")
endpoint := "http://localhost:8080/index.html"

c, err := getContent(endpoint, 120)
if err != nil {
t.Fatalf("failed to get content: %s", err)
Expand All @@ -255,21 +265,37 @@ func TestAll(t *testing.T) {
}

// Update content in token file
updatedContent := fmt.Sprintf("%d", time.Now().Unix())
updatedContent := fmt.Sprintf("%s-updated", name)
start := time.Now()
ioutil.WriteFile(contentPath, []byte(updatedContent), 0644)
time.Sleep(20 * time.Second)

log.Println("getting updated content")
c, err = getContent(endpoint, 120)
if err != nil {
t.Fatalf("failed to get updated content: %s", err)
if err := ioutil.WriteFile(contentPath, []byte(updatedContent), 0644); err != nil {
t.Fatalf("failed to update %s: %s", contentPath, err)
}

if c != updatedContent {
t.Fatalf("expected updated content to be %s, got %s", updatedContent, c)
log.Printf("getting updated content from %s\n", endpoint)
tick := time.NewTicker(1 * time.Second)
gotUpdated := false
for i := 0; i < 10; i++ {
<-tick.C
c, err = getContent(endpoint, 120)
if err != nil {
t.Fatalf("failed to get updated content: %s", err)
}

if c != updatedContent {
log.Printf("expected updated content to be %s, got %s\n", updatedContent, c)
continue
}

log.Printf("got updated content after %v\n", time.Now().Sub(start))
gotUpdated = true
break
}

log.Println("got updated content")
if !gotUpdated {
t.Fatal("never got the updated content")
}

if err := down(ctx, name, manifestPath, oktetoPath); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -330,14 +356,14 @@ func getContent(endpoint string, totRetries int) (string, error) {
return "", fmt.Errorf("failed to get %s: %w", endpoint, err)
}

log.Printf("Called %s, got %s, retrying", endpoint, err)
log.Printf("called %s, got %s, retrying", endpoint, err)
<-t.C
continue
}

defer r.Body.Close()
if r.StatusCode != 200 {
log.Printf("Called %s, got status %d, retrying", endpoint, r.StatusCode)
log.Printf("called %s, got status %d, retrying", endpoint, r.StatusCode)
<-t.C
continue
}
Expand Down Expand Up @@ -375,10 +401,13 @@ func createNamespace(ctx context.Context, oktetoPath, namespace string) error {
cmd.Env = os.Environ()
span, _ := process.InjectToCmdWithSpan(ctx, cmd)
defer span.Finish()
if o, err := cmd.CombinedOutput(); err != nil {
o, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%s %s: %s", oktetoPath, strings.Join(args, " "), string(o))
}

log.Printf("create namespace output: \n%s\n", string(o))

_, _, n, err := k8Client.GetLocal()
if err != nil {
return err
Expand All @@ -401,7 +430,7 @@ func deleteNamespace(ctx context.Context, oktetoPath, namespace string) error {
if err != nil {
return fmt.Errorf("okteto delete namespace failed: %s - %s", string(o), err)
}
k8Client.Reset()

return nil
}

Expand Down Expand Up @@ -472,11 +501,15 @@ func waitForUpExit(wg *sync.WaitGroup) error {
}

func waitForReady(namespace, name string) error {
state := fmt.Sprintf("%s/.okteto/%s/%s/okteto.state", os.Getenv("HOME"), namespace, name)
log.Println("waiting for okteto up to be ready")

state := path.Join(config.GetOktetoHome(), namespace, name, "okteto.state")

t := time.NewTicker(1 * time.Second)
for i := 0; i < 180; i++ {
c, err := ioutil.ReadFile(state)
if err != nil {
log.Printf("failed to read state file %s: %s", state, err)
if !os.IsNotExist(err) {
return err
}
Expand Down
28 changes: 21 additions & 7 deletions integration/stacks_test.go
Expand Up @@ -26,6 +26,7 @@ import (
"testing"
"time"

k8Client "github.com/okteto/okteto/pkg/k8s/client"
"go.undefinedlabs.com/scopeagent"
"go.undefinedlabs.com/scopeagent/instrumentation/process"
)
Expand Down Expand Up @@ -54,33 +55,49 @@ func TestStacks(t *testing.T) {

test := scopeagent.GetTest(t)
test.Run(tName, func(t *testing.T) {
log.Printf("running %s \n", tName)
k8Client.Reset()
if err := createNamespace(ctx, oktetoPath, namespace); err != nil {
t.Fatal(err)
}

log.Printf("created namespace %s \n", namespace)

if err := cloneGitRepo(ctx, stackGitRepo); err != nil {
t.Fatal(err)
}

log.Printf("cloned repo %s \n", stackGitRepo)

defer deleteGitRepo(ctx, stackGitFolder)

if err := deployStack(ctx, oktetoPath, stackManifest); err != nil {
t.Fatal(err)
}

log.Printf("deployed stack using %s \n", stackManifest)

endpoint := fmt.Sprintf("https://vote-%s.cloud.okteto.net", namespace)
content, err := getContent(endpoint, 150)
if err != nil {
t.Fatalf("failed to get stack content: %s", err)
}

if !strings.Contains(content, "Cats vs Dogs!") {
t.Fatalf("wrong stack content: %s", content)
}
if err := destroyStack(ctx, oktetoPath, stackManifest); err != nil {
t.Fatal(err)
}

log.Println("destroyed stack")

time.Sleep(5 * time.Second)
_, err = getDeployment(namespace, "vote")
if err == nil {
t.Fatalf("'vote' deployment not deleted after 'okteto stack destroy'")
}

if !strings.Contains(err.Error(), "not found") {
t.Fatalf("error getting deployment 'vote': %s", err.Error())
}
Expand All @@ -106,15 +123,12 @@ func cloneGitRepo(ctx context.Context, name string) error {

func deleteGitRepo(ctx context.Context, path string) error {
log.Printf("delete git repo %s", path)
cmd := exec.Command("rm", "-Rf", path)
cmd.Env = os.Environ()
span, _ := process.InjectToCmdWithSpan(ctx, cmd)
defer span.Finish()
o, err := cmd.CombinedOutput()
err := os.RemoveAll(path)
if err != nil {
return fmt.Errorf("delete git repo %s failed: %s - %s", path, string(o), err)
return fmt.Errorf("delete git repo %s failed: %w", path, err)
}
log.Printf("delete git repo %s success", path)

log.Printf("deleted git repo %s", path)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/k8s/deployments/translate.go
Expand Up @@ -70,8 +70,8 @@ func translate(t *model.Translation, ns *apiv1.Namespace, c *kubernetes.Clientse
t.Deployment.GetObjectMeta().SetAnnotations(annotations)

if c != nil && namespaces.IsOktetoNamespace(ns) {
_, v := os.LookupEnv("OKTETO_CLIENTSIDE_TRANSLATION")
if !v {
c := os.Getenv("OKTETO_CLIENTSIDE_TRANSLATION")
if c == "" {
commonTranslation(t)
return setTranslationAsAnnotation(t.Deployment.Spec.Template.GetObjectMeta(), t)
}
Expand Down

0 comments on commit 4396101

Please sign in to comment.