Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrew Reed
committed
Feb 12, 2021
1 parent
529fe8c
commit 40af0f8
Showing
9 changed files
with
336 additions
and
1 deletion.
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,25 @@ | ||
apiVersion: troubleshoot.sh/v1beta2 | ||
kind: HostPreflight | ||
metadata: | ||
name: connect | ||
spec: | ||
collectors: | ||
- tcpConnect: | ||
collectorName: weave host 1 | ||
address: 10.128.0.2:6783 | ||
analyzers: | ||
- tcpConnect: | ||
collectorName: weave host 1 | ||
outcomes: | ||
- fail: | ||
when: "connection-refused" | ||
message: Connection to weave on host 1 was refused | ||
- fail: | ||
when: "connection-timeout" | ||
message: Timed out connecting to weave on host 1 | ||
- fail: | ||
when: "error" | ||
message: Unexpected error connecting to weave on host 1 | ||
- pass: | ||
when: "connected" | ||
message: Successfully connected to weave on host 1 |
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,86 @@ | ||
package analyzer | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"path" | ||
|
||
"github.com/pkg/errors" | ||
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" | ||
"github.com/replicatedhq/troubleshoot/pkg/collect" | ||
) | ||
|
||
func analyzeHostTCPConnect(hostAnalyzer *troubleshootv1beta2.TCPConnectAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) { | ||
fullPath := path.Join("connect", fmt.Sprintf("%s.json", hostAnalyzer.CollectorName)) | ||
|
||
collected, err := getCollectedFileContents(fullPath) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to read collected file name: %s", fullPath) | ||
} | ||
actual := collect.NetworkStatusResult{} | ||
if err := json.Unmarshal(collected, &actual); err != nil { | ||
return nil, errors.Wrap(err, "failed to unmarshal collected") | ||
} | ||
|
||
result := AnalyzeResult{} | ||
|
||
title := hostAnalyzer.CheckName | ||
if title == "" { | ||
title = "TCP Connection Attempt" | ||
} | ||
result.Title = title | ||
|
||
for _, outcome := range hostAnalyzer.Outcomes { | ||
if outcome.Fail != nil { | ||
if outcome.Fail.When == "" { | ||
result.IsFail = true | ||
result.Message = outcome.Fail.Message | ||
result.URI = outcome.Fail.URI | ||
|
||
return &result, nil | ||
} | ||
|
||
if string(actual.Status) == outcome.Fail.When { | ||
result.IsFail = true | ||
result.Message = outcome.Fail.Message | ||
result.URI = outcome.Fail.URI | ||
|
||
return &result, nil | ||
} | ||
} else if outcome.Warn != nil { | ||
if outcome.Warn.When == "" { | ||
result.IsWarn = true | ||
result.Message = outcome.Warn.Message | ||
result.URI = outcome.Warn.URI | ||
|
||
return &result, nil | ||
} | ||
|
||
if string(actual.Status) == outcome.Warn.When { | ||
result.IsWarn = true | ||
result.Message = outcome.Warn.Message | ||
result.URI = outcome.Warn.URI | ||
|
||
return &result, nil | ||
} | ||
} else if outcome.Pass != nil { | ||
if outcome.Pass.When == "" { | ||
result.IsPass = true | ||
result.Message = outcome.Pass.Message | ||
result.URI = outcome.Pass.URI | ||
|
||
return &result, nil | ||
} | ||
|
||
if string(actual.Status) == outcome.Pass.When { | ||
result.IsPass = true | ||
result.Message = outcome.Pass.Message | ||
result.URI = outcome.Pass.URI | ||
|
||
return &result, nil | ||
} | ||
} | ||
} | ||
|
||
return &result, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package analyzer | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" | ||
"github.com/replicatedhq/troubleshoot/pkg/collect" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAnalyzeTCPConnect(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
info *collect.NetworkStatusResult | ||
hostAnalyzer *troubleshootv1beta2.TCPConnectAnalyze | ||
result *AnalyzeResult | ||
expectErr bool | ||
}{ | ||
{ | ||
name: "connection refused, fail", | ||
info: &collect.NetworkStatusResult{ | ||
Status: collect.NetworkStatusConnectionRefused, | ||
}, | ||
hostAnalyzer: &troubleshootv1beta2.TCPConnectAnalyze{ | ||
Outcomes: []*troubleshootv1beta2.Outcome{ | ||
{ | ||
Fail: &troubleshootv1beta2.SingleOutcome{ | ||
When: "connection-refused", | ||
Message: "Connection was refused", | ||
}, | ||
}, | ||
}, | ||
}, | ||
result: &AnalyzeResult{ | ||
Title: "TCP Connection Attempt", | ||
IsFail: true, | ||
Message: "Connection was refused", | ||
}, | ||
}, | ||
{ | ||
name: "connected, fail", | ||
info: &collect.NetworkStatusResult{ | ||
Status: collect.NetworkStatusConnected, | ||
}, | ||
hostAnalyzer: &troubleshootv1beta2.TCPConnectAnalyze{ | ||
Outcomes: []*troubleshootv1beta2.Outcome{ | ||
{ | ||
Fail: &troubleshootv1beta2.SingleOutcome{ | ||
When: "connection-refused", | ||
Message: "Connection was refused", | ||
}, | ||
}, | ||
{ | ||
Pass: &troubleshootv1beta2.SingleOutcome{ | ||
When: "connected", | ||
Message: "Connection was successful", | ||
}, | ||
}, | ||
}, | ||
}, | ||
result: &AnalyzeResult{ | ||
Title: "TCP Connection Attempt", | ||
IsPass: true, | ||
Message: "Connection was successful", | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
req := require.New(t) | ||
b, err := json.Marshal(test.info) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
getCollectedFileContents := func(filename string) ([]byte, error) { | ||
return b, nil | ||
} | ||
|
||
result, err := analyzeHostTCPConnect(test.hostAnalyzer, getCollectedFileContents) | ||
if test.expectErr { | ||
req.Error(err) | ||
} else { | ||
req.NoError(err) | ||
} | ||
|
||
assert.Equal(t, test.result, result) | ||
}) | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.