|
| 1 | +package admupgradestatus |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/openshift/origin/pkg/test/ginkgo/junitapi" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + operatorLinePattern = regexp.MustCompile(`^\S+\s+\S+\s+\S+\s+.*$`) |
| 14 | +) |
| 15 | + |
| 16 | +func (w *monitor) controlPlane() *junitapi.JUnitTestCase { |
| 17 | + controlPlane := &junitapi.JUnitTestCase{ |
| 18 | + Name: "[sig-cli][OCPFeatureGate:UpgradeStatus] oc adm upgrade status control plane section is consistent", |
| 19 | + SkipMessage: &junitapi.SkipMessage{ |
| 20 | + Message: "Test skipped because no oc adm upgrade status output was successfully collected", |
| 21 | + }, |
| 22 | + } |
| 23 | + |
| 24 | + failureOutputBuilder := strings.Builder{} |
| 25 | + |
| 26 | + for _, observed := range w.ocAdmUpgradeStatusOutputModels { |
| 27 | + if observed.output == nil { |
| 28 | + // Failing to parse the output is handled in expectedLayout, so we can skip here |
| 29 | + continue |
| 30 | + } |
| 31 | + // We saw at least one successful execution of oc adm upgrade status, so we have data to process |
| 32 | + controlPlane.SkipMessage = nil |
| 33 | + |
| 34 | + wroteOnce := false |
| 35 | + fail := func(message string) { |
| 36 | + if !wroteOnce { |
| 37 | + wroteOnce = true |
| 38 | + failureOutputBuilder.WriteString(fmt.Sprintf("\n===== %s\n", observed.when.Format(time.RFC3339))) |
| 39 | + failureOutputBuilder.WriteString(observed.output.rawOutput) |
| 40 | + failureOutputBuilder.WriteString(fmt.Sprintf("\n\n=> %s\n", message)) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if !observed.output.updating { |
| 45 | + // If the cluster is not updating, control plane should not be updating |
| 46 | + if observed.output.controlPlane != nil { |
| 47 | + fail("Cluster is not updating but control plane section is present") |
| 48 | + } |
| 49 | + continue |
| 50 | + } |
| 51 | + |
| 52 | + cp := observed.output.controlPlane |
| 53 | + if cp == nil { |
| 54 | + fail("Cluster is updating but control plane section is not present") |
| 55 | + continue |
| 56 | + } |
| 57 | + |
| 58 | + if cp.Updated { |
| 59 | + for message, condition := range map[string]bool{ |
| 60 | + "Control plane is reported updated but summary section is present": cp.Summary != nil, |
| 61 | + "Control plane is reported updated but operators section is present": cp.Operators != nil, |
| 62 | + "Control plane is reported updated but nodes section is present": cp.Nodes != nil, |
| 63 | + "Control plane is reported updated but nodes are not updated": !cp.NodesUpdated, |
| 64 | + } { |
| 65 | + if condition { |
| 66 | + fail(message) |
| 67 | + } |
| 68 | + } |
| 69 | + continue |
| 70 | + } |
| 71 | + |
| 72 | + if cp.Summary == nil { |
| 73 | + fail("Control plane is not updated but summary section is not present") |
| 74 | + } |
| 75 | + |
| 76 | + for _, key := range []string{"Assessment", "Target Version", "Completion", "Duration", "Operator Health"} { |
| 77 | + value, ok := cp.Summary[key] |
| 78 | + if !ok { |
| 79 | + fail(fmt.Sprintf("Control plane summary does not contain %s", key)) |
| 80 | + } else if value == "" { |
| 81 | + fail(fmt.Sprintf("%s is empty", key)) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + updatingOperators, ok := cp.Summary["Updating"] |
| 86 | + if !ok { |
| 87 | + if cp.Operators != nil { |
| 88 | + fail("Control plane summary does not contain Updating key but operators section is present") |
| 89 | + continue |
| 90 | + } |
| 91 | + } else { |
| 92 | + if updatingOperators == "" { |
| 93 | + fail("Control plane summary contains Updating key but it is empty") |
| 94 | + continue |
| 95 | + } |
| 96 | + |
| 97 | + if cp.Operators == nil { |
| 98 | + fail("Control plane summary contains Updating key but operators section is not present") |
| 99 | + continue |
| 100 | + } |
| 101 | + |
| 102 | + items := len(strings.Split(updatingOperators, ",")) |
| 103 | + // TODO: These should actually exactly match, but `oc adm upgrade status` emits operators with linebreaks in |
| 104 | + // messages in a crappy way which we will need to fix |
| 105 | + if len(cp.Operators) < items { |
| 106 | + fail(fmt.Sprintf("Control plane summary contains Updating key with %d operators but operators section has %d items", items, len(cp.Operators))) |
| 107 | + continue |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // TODO: `oc adm upgrade status` emits operators with linebreaks in messages in a crappy way which we will need to fix |
| 112 | + // for _, operator := range cp.Operators { |
| 113 | + // if !operatorLinePattern.MatchString(operator) { |
| 114 | + // fail(fmt.Sprintf("Bad line in operators: %s", operator)) |
| 115 | + // } |
| 116 | + // } |
| 117 | + |
| 118 | + for _, node := range cp.Nodes { |
| 119 | + if !nodeLinePattern.MatchString(node) { |
| 120 | + fail(fmt.Sprintf("Bad line in nodes: %s", node)) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if failureOutputBuilder.Len() > 0 { |
| 126 | + controlPlane.FailureOutput = &junitapi.FailureOutput{ |
| 127 | + Message: fmt.Sprintf("observed unexpected outputs in oc adm upgrade status control plane section"), |
| 128 | + Output: failureOutputBuilder.String(), |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return controlPlane |
| 133 | +} |
0 commit comments