-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from akshaymankar/integration-tests
Add Integration tests
- Loading branch information
Showing
12 changed files
with
461 additions
and
78 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
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,4 @@ | ||
dist | ||
dist-newstyle | ||
*.cabal | ||
.stack-work |
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 @@ | ||
../LICENSE |
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,5 @@ | ||
FROM ubuntu:xenial | ||
|
||
RUN apt-get update && apt-get install -y libgmp3-dev | ||
|
||
COPY in-cluster-example /usr/local/bin |
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,85 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Main where | ||
|
||
import Control.Concurrent.STM | ||
import Control.Exception.Safe | ||
import Kubernetes.Client | ||
import Kubernetes.OpenAPI | ||
import Kubernetes.OpenAPI.API.CoreV1 | ||
import Network.HTTP.Client | ||
import Network.HTTP.Types.Status | ||
|
||
import qualified Data.Map as Map | ||
import qualified Data.Text as T | ||
import qualified Data.Text.IO as T | ||
|
||
main :: IO () | ||
main = do | ||
oidcCache <- newTVarIO $ Map.fromList [] | ||
(manager, cfg) <- mkKubeClientConfig oidcCache KubeConfigCluster | ||
let createNamespaceRequest = | ||
createNamespace (ContentType MimeJSON) (Accept MimeJSON) testNamespace | ||
createdNS <- assertMimeSuccess =<< dispatchMime manager cfg createNamespaceRequest | ||
nsName <- assertJust "Expected K8s to generate name for namespace, but it didn't" | ||
$ (v1ObjectMetaName =<< v1NamespaceMetadata createdNS) | ||
T.putStrLn $ "Created Namespace: " <> nsName | ||
|
||
-- NOTE: We cannot use dispatchMime due to this issue: https://github.com/kubernetes/kubernetes/issues/59501 | ||
let deleteNamespaceRequest = | ||
deleteNamespace (ContentType MimeJSON) (Accept MimeJSON) (Name nsName) | ||
deleteNamespaceResponse <- dispatchLbs manager cfg deleteNamespaceRequest | ||
if responseStatus deleteNamespaceResponse /= status200 | ||
then throwM $ AssertionFailure | ||
$ "Failed to cleanup namespace: " <> T.unpack nsName | ||
<> "\nStatus Code: " <> show (responseStatus deleteNamespaceResponse) | ||
<> "\nBody: " <> show (responseBody deleteNamespaceResponse) | ||
else return () | ||
putStrLn "Clenaup complete!" | ||
|
||
testDeployment :: V1Deployment | ||
testDeployment = | ||
let labelSelector = | ||
mkV1LabelSelector | ||
{ v1LabelSelectorMatchLabels = | ||
Just $ Map.fromList [("app", "test")] } | ||
container = | ||
(mkV1Container "container-name") | ||
{ v1ContainerImage = Just $ "nginx" } | ||
podTemplate = | ||
mkV1PodTemplateSpec | ||
{ v1PodTemplateSpecMetadata = | ||
Just $ mkV1ObjectMeta | ||
{ v1ObjectMetaLabels = Just $ Map.fromList [("app", "test")] } | ||
, v1PodTemplateSpecSpec = | ||
Just $ | ||
mkV1PodSpec [container] | ||
} | ||
in mkV1Deployment | ||
{ v1DeploymentMetadata = | ||
Just $ mkV1ObjectMeta { v1ObjectMetaName = Just "test-deployment" } | ||
, v1DeploymentSpec = | ||
Just | ||
$ (mkV1DeploymentSpec labelSelector podTemplate) | ||
} | ||
|
||
testNamespace :: V1Namespace | ||
testNamespace = | ||
let nsMetadata = | ||
mkV1ObjectMeta | ||
{ v1ObjectMetaGenerateName = Just "haskell-client-test-" } | ||
in mkV1Namespace | ||
{ v1NamespaceMetadata = Just nsMetadata } | ||
|
||
assertMimeSuccess :: MonadThrow m => MimeResult a -> m a | ||
assertMimeSuccess (MimeResult (Right res) _) = pure res | ||
assertMimeSuccess (MimeResult (Left err) _) = | ||
throwM $ AssertionFailure $ "Unexpected MimeError: " ++ show err | ||
|
||
assertJust :: MonadThrow m => String -> Maybe a -> m a | ||
assertJust err Nothing = throwM $ AssertionFailure err | ||
assertJust _ (Just x) = return x | ||
|
||
data AssertionFailure = AssertionFailure String | ||
deriving Show | ||
|
||
instance Exception AssertionFailure |
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,68 @@ | ||
#!/bin/bash | ||
|
||
set -euo pipefail | ||
|
||
SCRIPT_DIR="$( cd "$(dirname "$0")" ; pwd -P )" | ||
MAX_SECONDS=20 | ||
|
||
main(){ | ||
kubectl apply -f "$SCRIPT_DIR/test-pod.yaml" | ||
start_time="$(date +%s)" | ||
while true; do | ||
phase="$(get-pod-phase in-cluster-example)" | ||
consumed_seconds="$(seconds-since $start_time)" | ||
|
||
if [[ "$phase" == "Succeeded" ]]; then | ||
echo "------------------------------" | ||
echo "Test passed!" | ||
echo "------------------------------" | ||
echo | ||
echo "------------------------------" | ||
echo "Logs from test:" | ||
echo "------------------------------" | ||
kubectl logs in-cluster-example | ||
exit 0 | ||
elif [[ "$phase" == "Failed" ]]; then | ||
echo "------------------------------" | ||
echo "Test failed!" | ||
echo "------------------------------" | ||
print-failure in-cluster-example | ||
exit 1 | ||
elif (( consumed_seconds > MAX_SECONDS )); then | ||
echo "------------------------------" | ||
echo "Test timed out after $MAX_SECONDS seconds!" | ||
echo "------------------------------" | ||
print-failure in-cluster-example | ||
exit 2 | ||
else | ||
echo "Test still running, pod phase = $phase" | ||
sleep 0.5 | ||
fi | ||
done | ||
} | ||
|
||
get-pod-phase() { | ||
kubectl get pod $1 -o 'jsonpath={.status.phase}' | ||
} | ||
|
||
print-failure() { | ||
echo | ||
echo "------------------------------" | ||
echo "Pod Description:" | ||
echo "------------------------------" | ||
kubectl describe pod $1 | ||
echo | ||
echo "------------------------------" | ||
echo "Logs from test:" | ||
echo "------------------------------" | ||
kubectl logs $1 | ||
} | ||
|
||
# Takes epoch time | ||
seconds-since() { | ||
local start=$1 | ||
local end=$(date +%s) | ||
echo $(( end - start)) | ||
} | ||
|
||
main |
Oops, something went wrong.