This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Cancel integration tests when mismatch between CLI and kube co…
…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
Showing
2 changed files
with
57 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters