github.com/google/cloudprober@v0.11.3/targets/rtc/rtcservice/rtcservice_test.go (about)

     1  // Copyright 2017 The Cloudprober 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 rtcservice
    16  
    17  import (
    18  	"fmt"
    19  	"sort"
    20  	"testing"
    21  
    22  	"github.com/kylelemons/godebug/pretty"
    23  )
    24  
    25  const (
    26  	Write = iota
    27  	Delete
    28  )
    29  
    30  type Action int
    31  
    32  // Small regression tests for the RTC Stub object. Checks that read, writes,
    33  // and deletes all are working.
    34  func TestStub(t *testing.T) {
    35  	s := NewStub()
    36  	var actions = []struct {
    37  		act         Action
    38  		key         string
    39  		val         string
    40  		expectError bool
    41  		want        []string
    42  	}{
    43  		{Write, "k1", "v1", false,
    44  			[]string{"k1 : v1"}},
    45  		{Write, "k2", "v2", false,
    46  			[]string{"k1 : v1", "k2 : v2"}},
    47  		{Write, "k1", "v2", false,
    48  			[]string{"k1 : v2", "k2 : v2"}},
    49  		{Delete, "k2", "", false,
    50  			[]string{"k1 : v2"}},
    51  		{Delete, "k3", "", true,
    52  			[]string{"k1 : v2"}},
    53  	}
    54  
    55  	for id, a := range actions {
    56  		// Perform action
    57  		var err error
    58  		switch a.act {
    59  		case Write:
    60  			err = s.Write(a.key, []byte(a.val))
    61  		case Delete:
    62  			err = s.Delete(a.key)
    63  		default:
    64  			t.Error("In test row ", id, ": a.act = %v, want Write (%v) or Delete (%v).", a.act, Write, Delete)
    65  			continue
    66  		}
    67  
    68  		// Check for error
    69  		if (err != nil) != a.expectError {
    70  			t.Error("In test row ", id, ": Action err = %v, want %v.", err, a.expectError)
    71  			continue
    72  		}
    73  
    74  		// Check all rtc vars
    75  		gotVars, err := s.List()
    76  		if err != nil {
    77  			t.Error("In test row ", id, ": s.List() err = %v, want nil. ", err)
    78  		}
    79  		got := make([]string, len(gotVars))
    80  		for i, g := range gotVars {
    81  			k := g.Name
    82  			v, err := s.Val(g)
    83  			if err != nil {
    84  				t.Errorf("In test row %v : s.Val(%#v) erred. %v", id, g, err)
    85  				continue
    86  			}
    87  			got[i] = fmt.Sprintf("%v : %v", k, string(v))
    88  		}
    89  		sort.Strings(got)
    90  		sort.Strings(a.want)
    91  		if diff := pretty.Compare(a.want, got); diff != "" {
    92  			t.Errorf("In test row %v : rtc.List() got diff:\n%s", id, diff)
    93  		}
    94  	}
    95  }