mirror of
https://github.com/replicatedhq/troubleshoot.git
synced 2026-02-14 18:29:53 +00:00
fix(analyzer): fix clusterResource analyzer always uses the first object found (#1185)
* fix(analyzer): fix clusterResource analyzer always uses the first object found
This commit is contained in:
@@ -94,11 +94,11 @@ func FindResource(kind string, clusterScoped bool, namespace string, name string
|
||||
}
|
||||
itemslice := items.([]interface{})
|
||||
for _, item := range itemslice {
|
||||
name, err := iutils.GetAtPath(item, "metadata.name")
|
||||
resourceName, err := iutils.GetAtPath(item, "metadata.name")
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "Failed to find resource with name: %s", name)
|
||||
}
|
||||
if name == name {
|
||||
if resourceName == name {
|
||||
return item, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,12 +10,14 @@ import (
|
||||
|
||||
func Test_clusterResource(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
isError bool
|
||||
analyzer troubleshootv1beta2.ClusterResource
|
||||
name string
|
||||
isError bool
|
||||
resourceExists bool
|
||||
analyzer troubleshootv1beta2.ClusterResource
|
||||
}{
|
||||
{
|
||||
name: "namespaced resource",
|
||||
name: "namespaced resource",
|
||||
resourceExists: true,
|
||||
analyzer: troubleshootv1beta2.ClusterResource{
|
||||
CollectorName: "Check namespaced resource",
|
||||
Kind: "Deployment",
|
||||
@@ -24,7 +26,8 @@ func Test_clusterResource(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "check default fallthrough",
|
||||
name: "check default fallthrough",
|
||||
resourceExists: true,
|
||||
analyzer: troubleshootv1beta2.ClusterResource{
|
||||
CollectorName: "Check namespaced resource",
|
||||
Kind: "Deployment",
|
||||
@@ -32,7 +35,8 @@ func Test_clusterResource(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "cluster scoped resource",
|
||||
name: "cluster scoped resource",
|
||||
resourceExists: true,
|
||||
analyzer: troubleshootv1beta2.ClusterResource{
|
||||
CollectorName: "Check namespaced resource",
|
||||
Kind: "Node",
|
||||
@@ -40,6 +44,16 @@ func Test_clusterResource(t *testing.T) {
|
||||
Name: "repldev-marc",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "resource does not exist",
|
||||
resourceExists: false,
|
||||
analyzer: troubleshootv1beta2.ClusterResource{
|
||||
CollectorName: "Check namespaced resource",
|
||||
Kind: "Node",
|
||||
ClusterScoped: true,
|
||||
Name: "resource-does-not-exist",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
@@ -49,9 +63,9 @@ func Test_clusterResource(t *testing.T) {
|
||||
fcp := fileContentProvider{rootDir: rootDir}
|
||||
|
||||
analyzer := &test.analyzer
|
||||
_, err := FindResource(analyzer.Kind, analyzer.ClusterScoped, analyzer.Namespace, analyzer.Name, fcp.getFileContents)
|
||||
item, err := FindResource(analyzer.Kind, analyzer.ClusterScoped, analyzer.Namespace, analyzer.Name, fcp.getFileContents)
|
||||
assert.Equal(t, test.resourceExists, item != nil)
|
||||
assert.Nil(t, err)
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user