Skip to content

Commit

Permalink
chore: Add unit tests for the shared health resource feature and fix …
Browse files Browse the repository at this point in the history
…docs (argoproj#12715) (argoproj#12720)

* Dummy action in

Signed-off-by: reggie <reginakagan@gmail.com>

* Dummy action in

Signed-off-by: reggie <reginakagan@gmail.com>

* happy happy joy joy

Signed-off-by: reggie <reginakagan@gmail.com>

* will the tests fail?

Signed-off-by: reggie <reginakagan@gmail.com>

* happy happy joy joy

Signed-off-by: reggie <reginakagan@gmail.com>

* lua tests with relative path

Signed-off-by: reggie <reginakagan@gmail.com>

* bye bye custom action

Signed-off-by: reggie <reginakagan@gmail.com>

* placatin custom actions tests

Signed-off-by: reggie <reginakagan@gmail.com>

* added tests and fixed docs

Signed-off-by: reggie <reginakagan@gmail.com>

* added tests and fixed docs

Signed-off-by: reggie <reginakagan@gmail.com>

* Update docs/operator-manual/health.md

Co-authored-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>
Signed-off-by: reggie-k <reginakagan@gmail.com>

---------

Signed-off-by: reggie <reginakagan@gmail.com>
Signed-off-by: reggie-k <reginakagan@gmail.com>
Co-authored-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>
  • Loading branch information
2 people authored and xiaowu.zhu committed Aug 9, 2023
1 parent b121231 commit 255f017
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 2 deletions.
5 changes: 3 additions & 2 deletions docs/operator-manual/health.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ In order to prevent duplication of the custom health check for potentially multi

```yaml
resource.customizations: |
*.aws.crossplane.io/*:
"*.aws.crossplane.io/*":
health.lua: |
...
```


!!!important
Please note the required quotes in the resource customization health section, if the wildcard starts with `*`.

The `obj` is a global variable which contains the resource. The script must return an object with status and optional message field.
The custom health check might return one of the following health statuses:
Expand Down
82 changes: 82 additions & 0 deletions util/lua/lua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ metadata:
resourceVersion: "123"
`

const ec2AWSCrossplaneObjJson = `
apiVersion: ec2.aws.crossplane.io/v1alpha1
kind: Instance
metadata:
name: sample-crosspalne-ec2-instance
spec:
forProvider:
region: us-west-2
instanceType: t2.micro
keyName: my-crossplane-key-pair
providerConfigRef:
name: awsconfig
`

const newHealthStatusFunction = `a = {}
a.status = "Healthy"
a.message ="NeedsToBeChanged"
Expand All @@ -43,6 +57,14 @@ if obj.metadata.name == "helm-guestbook" then
end
return a`

const newWildcardHealthStatusFunction = `a = {}
a.status = "Healthy"
a.message ="NeedsToBeChanged"
if obj.metadata.name == "sample-crosspalne-ec2-instance" then
a.message = "testWildcardMessage"
end
return a`

func StrToUnstructured(jsonStr string) *unstructured.Unstructured {
obj := make(map[string]interface{})
err := yaml.Unmarshal([]byte(jsonStr), &obj)
Expand All @@ -65,6 +87,19 @@ func TestExecuteNewHealthStatusFunction(t *testing.T) {

}

func TestExecuteWildcardHealthStatusFunction(t *testing.T) {
testObj := StrToUnstructured(ec2AWSCrossplaneObjJson)
vm := VM{}
status, err := vm.ExecuteHealthLua(testObj, newWildcardHealthStatusFunction)
assert.Nil(t, err)
expectedHealthStatus := &health.HealthStatus{
Status: "Healthy",
Message: "testWildcardMessage",
}
assert.Equal(t, expectedHealthStatus, status)

}

const osLuaScript = `os.getenv("HOME")`

func TestFailExternalLibCall(t *testing.T) {
Expand Down Expand Up @@ -160,6 +195,23 @@ func TestGetHealthScriptWithGroupWildcardOverride(t *testing.T) {
assert.Equal(t, newHealthStatusFunction, script)
}

func TestGetHealthScriptWithGroupAndKindWildcardOverride(t *testing.T) {
testObj := StrToUnstructured(ec2AWSCrossplaneObjJson)
vm := VM{
ResourceOverrides: map[string]appv1.ResourceOverride{
"*.aws.crossplane.io/*": {
HealthLua: newHealthStatusFunction,
UseOpenLibs: false,
},
},
}

script, useOpenLibs, err := vm.GetHealthScript(testObj)
assert.Nil(t, err)
assert.Equal(t, false, useOpenLibs)
assert.Equal(t, newHealthStatusFunction, script)
}

func TestGetHealthScriptPredefined(t *testing.T) {
testObj := StrToUnstructured(objJSON)
vm := VM{}
Expand Down Expand Up @@ -434,6 +486,11 @@ end
hs.status = "Healthy"
return hs`

const healthWildcardOverrideScript = `
hs = {}
hs.status = "Healthy"
return hs`

getHealthOverride := func(openLibs bool) ResourceHealthOverrides {
return ResourceHealthOverrides{
"ServiceAccount": appv1.ResourceOverride{
Expand All @@ -443,6 +500,12 @@ return hs`
}
}

getWildcardHealthOverride := ResourceHealthOverrides{
"*.aws.crossplane.io/*": appv1.ResourceOverride{
HealthLua: healthWildcardOverrideScript,
},
}

t.Run("Enable Lua standard lib", func(t *testing.T) {
testObj := StrToUnstructured(testSA)
overrides := getHealthOverride(true)
Expand All @@ -464,4 +527,23 @@ return hs`
assert.EqualError(t, err, expectedErr)
assert.Nil(t, status)
})

t.Run("Get resource health for wildcard override", func(t *testing.T) {
testObj := StrToUnstructured(ec2AWSCrossplaneObjJson)
overrides := getWildcardHealthOverride
status, err := overrides.GetResourceHealth(testObj)
assert.Nil(t, err)
expectedStatus := &health.HealthStatus{
Status: health.HealthStatusHealthy,
}
assert.Equal(t, expectedStatus, status)
})

t.Run("Resource health for wildcard override not found", func(t *testing.T) {
testObj := StrToUnstructured(testSA)
overrides := getWildcardHealthOverride
status, err := overrides.GetResourceHealth(testObj)
assert.Nil(t, err)
assert.Nil(t, status)
})
}
18 changes: 18 additions & 0 deletions util/settings/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,24 @@ func TestGetResourceOverrides(t *testing.T) {

}

func TestGetResourceOverridesHealthWithWildcard(t *testing.T) {
data := map[string]string{
"resource.customizations": `
"*.aws.crossplane.io/*":
health.lua: |
foo`,
}

t.Run("TestResourceHealthOverrideWithWildcard", func(t *testing.T) {
_, settingsManager := fixtures(data)

overrides, err := settingsManager.GetResourceOverrides()
assert.NoError(t, err)
assert.Equal(t, 2, len(overrides))
assert.Equal(t, "foo", overrides["*.aws.crossplane.io/*"].HealthLua)
})
}

func TestSettingsManager_GetResourceOverrides_with_empty_string(t *testing.T) {
_, settingsManager := fixtures(map[string]string{
resourceCustomizationsKey: "",
Expand Down

0 comments on commit 255f017

Please sign in to comment.