istio.io/istio@v0.0.0-20240520182934-d79c90f27776/operator/pkg/util/progress/progress_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package progress
    16  
    17  import (
    18  	"bytes"
    19  	"io"
    20  	"testing"
    21  
    22  	"istio.io/istio/operator/pkg/name"
    23  )
    24  
    25  func TestProgressLog(t *testing.T) {
    26  	buf := bytes.NewBuffer(nil)
    27  	testBuf := io.Writer(buf)
    28  	testWriter = &testBuf
    29  	expected := ""
    30  	expect := func(e string) {
    31  		t.Helper()
    32  		// In buffer mode we don't overwrite old data, so we are constantly appending to the expected
    33  		newExpected := expected + "\n" + e
    34  		if newExpected != buf.String() {
    35  			t.Fatalf("expected '%v', \ngot '%v'", newExpected, buf.String())
    36  		}
    37  		expected = newExpected
    38  	}
    39  
    40  	p := NewLog()
    41  	cnp := name.PilotComponentName
    42  	cnpo := name.UserFacingComponentName(cnp)
    43  	cnb := name.IstioBaseComponentName
    44  	cnbo := name.UserFacingComponentName(cnb)
    45  	foo := p.NewComponent(string(cnp))
    46  	foo.ReportProgress()
    47  	expect(`- Processing resources for ` + cnpo + `.`)
    48  
    49  	bar := p.NewComponent(string(cnb))
    50  	bar.ReportProgress()
    51  	// string buffer won't rewrite, so we append
    52  	expect(`- Processing resources for ` + cnbo + `, ` + cnpo + `.`)
    53  	bar.ReportProgress()
    54  	bar.ReportProgress()
    55  
    56  	bar.ReportWaiting([]string{"deployment"})
    57  	expect(`- Processing resources for ` + cnbo + `, ` + cnpo + `. Waiting for deployment`)
    58  
    59  	bar.ReportError("some error")
    60  	expect(`❌ ` + cnbo + ` encountered an error: some error`)
    61  
    62  	foo.ReportProgress()
    63  	expect(`- Processing resources for ` + cnpo + `.`)
    64  
    65  	foo.ReportFinished()
    66  	expect(`🧠 ` + cnpo + ` installed`)
    67  
    68  	p.SetState(StatePruning)
    69  	expect(`- Pruning removed resources`)
    70  
    71  	p.SetState(StateComplete)
    72  	expect(`✅ Installation complete`)
    73  
    74  	p.SetState(StateUninstallComplete)
    75  	expect(`✅ Uninstall complete`)
    76  }