|
1 | 1 | package analyzer |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" |
7 | | - "github.com/stretchr/testify/assert" |
8 | 8 | "github.com/stretchr/testify/require" |
9 | 9 | ) |
10 | 10 |
|
@@ -860,6 +860,12 @@ func TestAnalyzeHostFilesystemPerformance(t *testing.T) { |
860 | 860 | hostAnalyzer: &troubleshootv1beta2.FilesystemPerformanceAnalyze{ |
861 | 861 | CollectorName: "file system performance", |
862 | 862 | Outcomes: []*troubleshootv1beta2.Outcome{ |
| 863 | + { |
| 864 | + Warn: &troubleshootv1beta2.SingleOutcome{ |
| 865 | + When: "fileNotCollected", |
| 866 | + Message: "Warn when the file was not collected", |
| 867 | + }, |
| 868 | + }, |
863 | 869 | { |
864 | 870 | Pass: &troubleshootv1beta2.SingleOutcome{ |
865 | 871 | When: "p99 < 10ms", |
@@ -954,7 +960,136 @@ func TestAnalyzeHostFilesystemPerformance(t *testing.T) { |
954 | 960 | req.NoError(err) |
955 | 961 | } |
956 | 962 |
|
957 | | - assert.Equal(t, test.result, result) |
| 963 | + req.Equal(test.result, result) |
| 964 | + }) |
| 965 | + } |
| 966 | +} |
| 967 | + |
| 968 | +func TestAnalyzeHostFilesystemPerformanceNoFile(t *testing.T) { |
| 969 | + tests := []struct { |
| 970 | + name string |
| 971 | + hostAnalyzer *troubleshootv1beta2.FilesystemPerformanceAnalyze |
| 972 | + result []*AnalyzeResult |
| 973 | + expectErr bool |
| 974 | + }{ |
| 975 | + { |
| 976 | + name: "default behavior", |
| 977 | + hostAnalyzer: &troubleshootv1beta2.FilesystemPerformanceAnalyze{ |
| 978 | + CollectorName: "irrel", |
| 979 | + Outcomes: []*troubleshootv1beta2.Outcome{ |
| 980 | + { |
| 981 | + Fail: &troubleshootv1beta2.SingleOutcome{ |
| 982 | + Message: "a nonexistent file should not be analyzed", |
| 983 | + }, |
| 984 | + }, |
| 985 | + }, |
| 986 | + }, |
| 987 | + expectErr: true, |
| 988 | + }, |
| 989 | + { |
| 990 | + name: "fail case", |
| 991 | + hostAnalyzer: &troubleshootv1beta2.FilesystemPerformanceAnalyze{ |
| 992 | + CollectorName: "irrel", |
| 993 | + Outcomes: []*troubleshootv1beta2.Outcome{ |
| 994 | + { |
| 995 | + Fail: &troubleshootv1beta2.SingleOutcome{ |
| 996 | + When: "fileNotCollected", |
| 997 | + Message: "the required file was not collected", |
| 998 | + }, |
| 999 | + }, |
| 1000 | + }, |
| 1001 | + }, |
| 1002 | + expectErr: false, |
| 1003 | + result: []*AnalyzeResult{ |
| 1004 | + { |
| 1005 | + Title: "Filesystem Performance", |
| 1006 | + IsFail: true, |
| 1007 | + Message: "the required file was not collected", |
| 1008 | + }, |
| 1009 | + }, |
| 1010 | + }, |
| 1011 | + { |
| 1012 | + name: "warn case", |
| 1013 | + hostAnalyzer: &troubleshootv1beta2.FilesystemPerformanceAnalyze{ |
| 1014 | + CollectorName: "irrel", |
| 1015 | + Outcomes: []*troubleshootv1beta2.Outcome{ |
| 1016 | + { |
| 1017 | + Warn: &troubleshootv1beta2.SingleOutcome{ |
| 1018 | + When: "fileNotCollected", |
| 1019 | + Message: "the required file was not collected", |
| 1020 | + }, |
| 1021 | + }, |
| 1022 | + }, |
| 1023 | + }, |
| 1024 | + expectErr: false, |
| 1025 | + result: []*AnalyzeResult{ |
| 1026 | + { |
| 1027 | + Title: "Filesystem Performance", |
| 1028 | + IsWarn: true, |
| 1029 | + Message: "the required file was not collected", |
| 1030 | + }, |
| 1031 | + }, |
| 1032 | + }, |
| 1033 | + { |
| 1034 | + name: "pass case", |
| 1035 | + hostAnalyzer: &troubleshootv1beta2.FilesystemPerformanceAnalyze{ |
| 1036 | + CollectorName: "irrel", |
| 1037 | + Outcomes: []*troubleshootv1beta2.Outcome{ |
| 1038 | + { |
| 1039 | + Pass: &troubleshootv1beta2.SingleOutcome{ |
| 1040 | + When: "fileNotCollected", |
| 1041 | + Message: "the required file was not collected", |
| 1042 | + }, |
| 1043 | + }, |
| 1044 | + }, |
| 1045 | + }, |
| 1046 | + expectErr: false, |
| 1047 | + result: []*AnalyzeResult{ |
| 1048 | + { |
| 1049 | + Title: "Filesystem Performance", |
| 1050 | + IsPass: true, |
| 1051 | + Message: "the required file was not collected", |
| 1052 | + }, |
| 1053 | + }, |
| 1054 | + }, |
| 1055 | + { |
| 1056 | + name: "fileNotCollected is only tested if it is the first result", |
| 1057 | + hostAnalyzer: &troubleshootv1beta2.FilesystemPerformanceAnalyze{ |
| 1058 | + CollectorName: "irrel", |
| 1059 | + Outcomes: []*troubleshootv1beta2.Outcome{ |
| 1060 | + { |
| 1061 | + Fail: &troubleshootv1beta2.SingleOutcome{ |
| 1062 | + Message: "a nonexistent file should not be analyzed", |
| 1063 | + }, |
| 1064 | + }, |
| 1065 | + { |
| 1066 | + Pass: &troubleshootv1beta2.SingleOutcome{ |
| 1067 | + When: "fileNotCollected", |
| 1068 | + Message: "the required file was not collected", |
| 1069 | + }, |
| 1070 | + }, |
| 1071 | + }, |
| 1072 | + }, |
| 1073 | + expectErr: true, |
| 1074 | + }, |
| 1075 | + } |
| 1076 | + for _, test := range tests { |
| 1077 | + t.Run(test.name, func(t *testing.T) { |
| 1078 | + req := require.New(t) |
| 1079 | + |
| 1080 | + getCollectedFileContents := func(filename string) ([]byte, error) { |
| 1081 | + return nil, fmt.Errorf("file not found") |
| 1082 | + } |
| 1083 | + |
| 1084 | + a := AnalyzeHostFilesystemPerformance{test.hostAnalyzer} |
| 1085 | + result, err := a.Analyze(getCollectedFileContents, nil) |
| 1086 | + if test.expectErr { |
| 1087 | + req.Error(err) |
| 1088 | + } else { |
| 1089 | + req.NoError(err) |
| 1090 | + } |
| 1091 | + |
| 1092 | + req.Equal(test.result, result) |
958 | 1093 | }) |
959 | 1094 | } |
960 | 1095 | } |
0 commit comments