Skip to content

Commit

Permalink
Some tests on deterministic naming
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed Jul 19, 2019
1 parent 465f775 commit 642a1eb
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 7 deletions.
7 changes: 7 additions & 0 deletions pkg/apis/troubleshoot/v1beta1/collector_shared.go
Expand Up @@ -34,10 +34,17 @@ type Run struct {
ImagePullPolicy string `json:"imagePullPolicy,omitempty" yaml:"imagePullPolicy,omitempty"`
}

type Copy struct {
Selector []string `json:"selector" yaml:"selector"`
Namespace string `json:"namespace" yaml:"namespace"`
ContainerPath string `json:"containerPath" yaml:"containerPath"`
}

type Collect struct {
ClusterInfo *ClusterInfo `json:"clusterInfo,omitempty" yaml:"clusterInfo,omitempty"`
ClusterResources *ClusterResources `json:"clusterResources,omitempty" yaml:"clusterResources,omitempty"`
Secret *Secret `json:"secret,omitempty" yaml:"secret,omitempty"`
Logs *Logs `json:"logs,omitempty" yaml:"logs,omitempty"`
Run *Run `json:"run,omitempty" yaml:"run,omitempty"`
Copy *Copy `json:"copy,omitempty" yaml:"copy,omitempty"`
}
44 changes: 37 additions & 7 deletions pkg/collect/util.go
Expand Up @@ -3,26 +3,56 @@ package collect
import (
"fmt"
"strings"
"regexp"

troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
)

func DeterministicIDForCollector(collector *troubleshootv1beta1.Collect) string {
unsafeID := ""

if collector.ClusterInfo != nil {
return "cluster-info"
unsafeID = "cluster-info"
}

if collector.ClusterResources != nil {
return "cluster-resources"
unsafeID = "cluster-resources"
}

if collector.Secret != nil {
return fmt.Sprintf("secret-%s%s", collector.Secret.Namespace, collector.Secret.Name)
unsafeID = fmt.Sprintf("secret-%s-%s", collector.Secret.Namespace, collector.Secret.Name)
}

if collector.Logs != nil {
randomString := "abcdef" // TODO
return fmt.Sprintf("logs-%s%s", collector.Logs.Namespace, randomString)
unsafeID = fmt.Sprintf("logs-%s-%s", collector.Logs.Namespace, selectorToString(collector.Logs.Selector))
}

if collector.Run != nil {
return fmt.Sprintf("run-%s", strings.ToLower(collector.Run.Name))
unsafeID = fmt.Sprintf("run-%s", strings.ToLower(collector.Run.Name))
}
return ""

if collector.Copy != nil {
unsafeID = fmt.Sprintf("copy-%s-%s", selectorToString(collector.Copy.Selector), pathToString(collector.Copy.ContainerPath))
}

return rfc1035(unsafeID)
}

func selectorToString(selector []string) string {
return strings.Replace(strings.Join(selector, "-"), "=", "-", -1)
}

func pathToString(path string) string {
return strings.Replace(path, "/", "-", -1)
}

func rfc1035(in string) string {
reg := regexp.MustCompile("[^a-z0-9\\-]+")
out := reg.ReplaceAllString(in, "-")

if len(out) > 63 {
out = out[:63]
}

return out
}
80 changes: 80 additions & 0 deletions pkg/collect/util_test.go
@@ -0,0 +1,80 @@
package collect

import (
"testing"

"github.com/stretchr/testify/assert"
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
)

func Test_selectorToString(t *testing.T) {
tests := []struct {
name string
selector []string
expect string
}{
{
name: "app=api",
selector: []string{"app=api"},
expect: "app-api",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual := selectorToString(test.selector)
assert.Equal(t, test.expect, actual)
})
}
}

func Test_DeterministicIDForCollector(t *testing.T) {
tests := []struct {
name string
collector *troubleshootv1beta1.Collect
expect string
}{
{
name: "cluster-info",
collector: &troubleshootv1beta1.Collect{
ClusterInfo: &troubleshootv1beta1.ClusterInfo{},
},
expect: "cluster-info",
},
{
name: "cluster-resources",
collector: &troubleshootv1beta1.Collect{
ClusterResources: &troubleshootv1beta1.ClusterResources{},
},
expect: "cluster-resources",
},
{
name: "secret",
collector: &troubleshootv1beta1.Collect{
Secret: &troubleshootv1beta1.Secret{
Name: "secret-agent-woman",
Namespace: "top-secret",
},
},
expect: "secret-top-secret-secret-agent-woman",
},
{
name: "logs",
collector: &troubleshootv1beta1.Collect{
Logs: &troubleshootv1beta1.Logs{
Namespace: "top-secret",
Selector: []string{"this=is", "rather=long", "for=testing", "more=words", "too=many", "abcdef!=123456"},
},
},
expect: "logs-top-secret-this-is-rather-long-for-testing-more-words-too-",
},
}


for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual := DeterministicIDForCollector(test.collector)
assert.Equal(t, test.expect, actual)
})
}
}

0 comments on commit 642a1eb

Please sign in to comment.