github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/edge/pkg/metamanager/metamanager_test.go (about)

     1  /*
     2  Copyright 2018 The KubeEdge Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8     http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package metamanager
    18  
    19  //TODO Re-optimize testcase @kadisi
    20  /*
    21  
    22  import (
    23  	"testing"
    24  
    25  	"github.com/kubeedge/beehive/pkg/core"
    26  	"github.com/kubeedge/beehive/pkg/core/context"
    27  	"github.com/kubeedge/beehive/pkg/core/model"
    28  	"github.com/kubeedge/kubeedge/edge/pkg/common/dbm"
    29  	commodule "github.com/kubeedge/kubeedge/edge/pkg/common/modules"
    30  	"github.com/kubeedge/kubeedge/edge/pkg/devicetwin"
    31  	"github.com/kubeedge/kubeedge/edge/pkg/eventbus"
    32  	"github.com/kubeedge/kubeedge/edge/pkg/servicebus"
    33  )
    34  
    35  // coreContext is beehive context used for communication between modules
    36  var coreContext *context.Context
    37  
    38  // metaModule is metamanager implementation of Module interface
    39  var metaModule core.Module
    40  
    41  func init() {
    42  	devicetwin.Register()
    43  	eventbus.Register()
    44  	Register()
    45  	servicebus.Register()
    46  }
    47  
    48  // TestName will initialize CONFIG and register metaManager and test Name
    49  func TestName(t *testing.T) {
    50  	//Load Configurations as go test runs in /tmp
    51  	modules := core.GetModules()
    52  	core.Register(&metaManager{})
    53  	for name, module := range modules {
    54  		if name == MetaManagerModuleName {
    55  			metaModule = module
    56  			break
    57  		}
    58  	}
    59  	t.Run("ModuleRegistration", func(t *testing.T) {
    60  		if metaModule == nil {
    61  			t.Errorf("MetaManager Module not Registered with beehive core")
    62  			return
    63  		}
    64  		if MetaManagerModuleName != metaModule.Name() {
    65  			t.Errorf("Name of module is not correct wanted: %v and got: %v", MetaManagerModuleName, metaModule.Name())
    66  			return
    67  		}
    68  		if commodule.MetaGroup != metaModule.Group() {
    69  			t.Errorf("Group of module is not correct wanted: %v and got: %v", commodule.MetaGroup, metaModule.Group())
    70  		}
    71  	})
    72  
    73  }
    74  
    75  // TestStart is used for starting metaManager and testing if sync message is sent correctly
    76  func TestStart(t *testing.T) {
    77  	coreContext = context.GetContext(context.MsgCtxTypeChannel)
    78  	modules := core.GetModules()
    79  	for name, module := range modules {
    80  		coreContext.AddModule(name)
    81  		coreContext.AddModuleGroup(name, module.Group())
    82  	}
    83  	dbm.InitDBManager()
    84  	defer dbm.Cleanup()
    85  	go metaModule.Start(coreContext)
    86  
    87  	// wait to hit sync interval and receive message
    88  	message, err := coreContext.Receive(MetaManagerModuleName)
    89  	t.Run("TestMessageContent", func(t *testing.T) {
    90  		if err != nil {
    91  			t.Errorf("error while receiving message")
    92  			return
    93  		}
    94  		if (message.GetSource() != MetaManagerModuleName) || (message.GetGroup() != GroupResource) || (message.GetResource() != model.ResourceTypePodStatus) || (message.GetOperation() != OperationMetaSync) {
    95  			t.Errorf("Wrong message received")
    96  		}
    97  	})
    98  }
    99  
   100  // TestCleanup is function to test cleanup
   101  func TestCleanup(t *testing.T) {
   102  	metaModule.Cleanup()
   103  	var test model.Message
   104  
   105  	// Send message to avoid deadlock if channel deletion has failed after cleanup
   106  	go coreContext.Send(MetaManagerModuleName, test)
   107  
   108  	_, err := coreContext.Receive(MetaManagerModuleName)
   109  	t.Run("CheckCleanUp", func(t *testing.T) {
   110  		if err == nil {
   111  			t.Errorf("MetaManager Module still has channel after cleanup")
   112  		}
   113  	})
   114  }
   115  */