github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/configuration/revision_test.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package configuration
    21  
    22  import (
    23  	"fmt"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  
    28  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    29  	"github.com/1aal/kubeblocks/pkg/configuration/core"
    30  	"github.com/1aal/kubeblocks/pkg/constant"
    31  	"github.com/1aal/kubeblocks/pkg/controller/builder"
    32  	intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil"
    33  )
    34  
    35  func TestGcConfigRevision(t *testing.T) {
    36  	cm := builder.NewConfigMapBuilder("default", "test").
    37  		AddAnnotations(core.GenerateRevisionPhaseKey("1"), "Finished").
    38  		AddAnnotations(core.GenerateRevisionPhaseKey("2"), "Init").
    39  		AddAnnotations(core.GenerateRevisionPhaseKey("3"), "Finished").
    40  		AddAnnotations(core.GenerateRevisionPhaseKey("4"), "Finished").
    41  		GetObject()
    42  	revisions := GcRevision(cm.GetAnnotations())
    43  	assert.Equal(t, 0, len(revisions))
    44  
    45  	cm = builder.NewConfigMapBuilder("default", "test").
    46  		AddAnnotations(core.GenerateRevisionPhaseKey("1"), "Finished").
    47  		AddAnnotations(core.GenerateRevisionPhaseKey("2"), "Init").
    48  		AddAnnotations(core.GenerateRevisionPhaseKey("3"), "Finished").
    49  		AddAnnotations(core.GenerateRevisionPhaseKey("4"), "Finished").
    50  		AddAnnotations(core.GenerateRevisionPhaseKey("5"), "Finished").
    51  		AddAnnotations(core.GenerateRevisionPhaseKey("6"), "Finished").
    52  		AddAnnotations(core.GenerateRevisionPhaseKey("7"), "Finished").
    53  		AddAnnotations(core.GenerateRevisionPhaseKey("8"), "Finished").
    54  		AddAnnotations(core.GenerateRevisionPhaseKey("9"), "Finished").
    55  		AddAnnotations(core.GenerateRevisionPhaseKey("10"), "Finished").
    56  		AddAnnotations(core.GenerateRevisionPhaseKey("11"), "Finished").
    57  		AddAnnotations(core.GenerateRevisionPhaseKey("12"), `{"Phase":"Finished","Revision":"12","Policy":"","ExecResult":"","SucceedCount":0,"ExpectedCount":0,"Retry":false,"Failed":false,"Message":"the configuration file has not been modified, skip reconfigure"}`).
    58  		GetObject()
    59  
    60  	assert.Equal(t, 12, len(RetrieveRevision(cm.GetAnnotations())))
    61  
    62  	revisions = GcRevision(cm.GetAnnotations())
    63  	assert.Equal(t, 2, len(revisions))
    64  	assert.Equal(t, string(appsv1alpha1.CInitPhase), string(revisions[1].Phase))
    65  	assert.Equal(t, string(appsv1alpha1.CFinishedPhase), string(revisions[0].Phase))
    66  
    67  	GcConfigRevision(cm)
    68  	assert.Equal(t, 10, len(RetrieveRevision(cm.GetAnnotations())))
    69  }
    70  
    71  func TestParseRevision(t *testing.T) {
    72  	type args struct {
    73  		revision string
    74  		phase    string
    75  	}
    76  	tests := []struct {
    77  		name    string
    78  		args    args
    79  		want    ConfigurationRevision
    80  		wantErr bool
    81  	}{{
    82  		name: "test",
    83  		args: args{
    84  			revision: "12absdl",
    85  			phase:    "Init",
    86  		},
    87  		want:    ConfigurationRevision{},
    88  		wantErr: true,
    89  	}, {
    90  		name: "test",
    91  		args: args{
    92  			revision: "120000",
    93  			phase:    "Pending",
    94  		},
    95  		want: ConfigurationRevision{
    96  			StrRevision: "120000",
    97  			Revision:    120000,
    98  			Phase:       appsv1alpha1.CPendingPhase,
    99  			Result: intctrlutil.Result{
   100  				Phase:    appsv1alpha1.CPendingPhase,
   101  				Revision: "120000",
   102  			},
   103  		},
   104  		wantErr: false,
   105  	}, {
   106  		name: "test",
   107  		args: args{
   108  			revision: "",
   109  			phase:    "Init",
   110  		},
   111  		want:    ConfigurationRevision{},
   112  		wantErr: true,
   113  	}}
   114  	for _, tt := range tests {
   115  		t.Run(tt.name, func(t *testing.T) {
   116  			got, err := parseRevision(tt.args.revision, tt.args.phase)
   117  			if (err != nil) != tt.wantErr {
   118  				assert.Error(t, err, fmt.Sprintf("parseRevision(%v, %v)", tt.args.revision, tt.args.phase))
   119  			} else {
   120  				assert.Equalf(t, tt.want, got, "parseRevision(%v, %v)", tt.args.revision, tt.args.phase)
   121  			}
   122  		})
   123  	}
   124  }
   125  
   126  func TestGetCurrentRevision(t *testing.T) {
   127  	type args struct {
   128  		annotations map[string]string
   129  	}
   130  	tests := []struct {
   131  		name string
   132  		args args
   133  		want string
   134  	}{{
   135  		name: "test",
   136  		args: args{
   137  			annotations: map[string]string{},
   138  		},
   139  		want: "",
   140  	}, {
   141  		name: "test",
   142  		args: args{
   143  			annotations: map[string]string{"abcd": "finished"},
   144  		},
   145  		want: "",
   146  	}, {
   147  		name: "test",
   148  		args: args{
   149  			annotations: map[string]string{constant.ConfigurationRevision: "mytest"},
   150  		},
   151  		want: "mytest",
   152  	}}
   153  	for _, tt := range tests {
   154  		t.Run(tt.name, func(t *testing.T) {
   155  			assert.Equalf(t, tt.want, GetCurrentRevision(tt.args.annotations), "GetCurrentRevision(%v)", tt.args.annotations)
   156  		})
   157  	}
   158  }
   159  
   160  func TestGetLastRevision(t *testing.T) {
   161  	type args struct {
   162  		annotations map[string]string
   163  		revision    int64
   164  	}
   165  	tests := []struct {
   166  		name  string
   167  		args  args
   168  		want  ConfigurationRevision
   169  		want1 bool
   170  	}{{
   171  		name: "test",
   172  		args: args{
   173  			annotations: map[string]string{
   174  				core.GenerateRevisionPhaseKey("1"): "Finished",
   175  				core.GenerateRevisionPhaseKey("2"): "Running",
   176  			},
   177  			revision: 2,
   178  		},
   179  		want: ConfigurationRevision{
   180  			Revision:    2,
   181  			StrRevision: "2",
   182  			Phase:       appsv1alpha1.CRunningPhase,
   183  			Result: intctrlutil.Result{
   184  				Phase:    appsv1alpha1.CRunningPhase,
   185  				Revision: "2",
   186  			},
   187  		},
   188  		want1: true,
   189  	}, {
   190  		name: "test",
   191  		args: args{
   192  			annotations: map[string]string{
   193  				core.GenerateRevisionPhaseKey("1"): "Finished",
   194  				core.GenerateRevisionPhaseKey("2"): "Running",
   195  			},
   196  			revision: 3,
   197  		},
   198  		want1: false,
   199  	}}
   200  	for _, tt := range tests {
   201  		t.Run(tt.name, func(t *testing.T) {
   202  			got, got1 := GetLastRevision(tt.args.annotations, tt.args.revision)
   203  			assert.Equalf(t, tt.want, got, "GetLastRevision(%v, %v)", tt.args.annotations, tt.args.revision)
   204  			assert.Equalf(t, tt.want1, got1, "GetLastRevision(%v, %v)", tt.args.annotations, tt.args.revision)
   205  		})
   206  	}
   207  }