Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
chore: Cancel integration tests when mismatch between CLI and kube co…
Browse files Browse the repository at this point in the history
…ntext is detected (#5743) (#5824)

* chore: Cancel integration tests when mismatch between CLI and kube context is detected (#5734)

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>

* added comment

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
  • Loading branch information
bacherfl committed Nov 9, 2021
1 parent 079a6b4 commit 5596611
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
57 changes: 57 additions & 0 deletions test/go-tests/init_test.go
@@ -0,0 +1,57 @@
package go_tests

import (
"errors"
"fmt"
"os"
"strings"
"testing"
)

func TestMain(m *testing.M) {
if err := setup(); err != nil {
os.Exit(-1)
}
code := m.Run()
os.Exit(code)
}

func setup() error {
// before executing the tests, we check whether the context of the Keptn CLI matches the one of the kubectl CLI
// i.e. The kubectl CLI should be connected to the cluster the Keptn CLI is currently authenticated against.
// this prevents unintended kubectl commands from being executed against a different cluster than the one containing the Keptn instance that should be tested
match, err := endpointsMatch()
if err != nil {
return fmt.Errorf("could not compare endpoints of kubectl context and keptn CLI: %s", err.Error())
}
if !match {
return errors.New("endpoint mismatch between CLI and kubectl detected")
}
return nil
}

func endpointsMatch() (bool, error) {
_, keptnAPIURL, err := GetApiCredentials()
if err != nil {
return false, err
}
statusCmdOutput, err := ExecuteCommand("keptn status")
if err != nil {
return false, err
}
statusOutputLines := strings.Split(statusCmdOutput, "\n")

var apiURLFromStatusCommand string
for _, line := range statusOutputLines {
if strings.Contains(line, "Successfully authenticated") {
endpointLineSplit := strings.Split(line, " ")
apiURLFromStatusCommand = endpointLineSplit[len(endpointLineSplit)-1]
break
}
}

if apiURLFromStatusCommand != keptnAPIURL {
return false, nil
}
return true, nil
}
5 changes: 0 additions & 5 deletions test/go-tests/sequencetrigger_test.go
Expand Up @@ -30,11 +30,6 @@ spec:
- name: "mytask"
- name: "othertask"`

func TestMain(m *testing.M) {
code := m.Run()
os.Exit(code)
}

func Test_SequenceLoopIntegrationTest(t *testing.T) {
projectName := "sequence-loop"
serviceName := "my-service"
Expand Down

0 comments on commit 5596611

Please sign in to comment.