github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/core/chaincode/shim/shim_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 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 shim 18 19 import ( 20 "os" 21 "testing" 22 23 "github.com/op/go-logging" 24 ) 25 26 // Test Go shim functionality that can be tested outside of a real chaincode 27 // context. 28 29 // TestShimLogging simply tests that the APIs are working. These tests test 30 // for correct control over the shim's logging object and the LogLevel 31 // function. 32 func TestShimLogging(t *testing.T) { 33 SetLoggingLevel(LogCritical) 34 if shimLoggingLevel != LogCritical { 35 t.Errorf("shimLoggingLevel is not LogCritical as expected") 36 } 37 if chaincodeLogger.IsEnabledFor(logging.DEBUG) { 38 t.Errorf("The chaincodeLogger should not be enabled for DEBUG") 39 } 40 if !chaincodeLogger.IsEnabledFor(logging.CRITICAL) { 41 t.Errorf("The chaincodeLogger should be enabled for CRITICAL") 42 } 43 var level LoggingLevel 44 var err error 45 level, err = LogLevel("debug") 46 if err != nil { 47 t.Errorf("LogLevel(debug) failed") 48 } 49 if level != LogDebug { 50 t.Errorf("LogLevel(debug) did not return LogDebug") 51 } 52 level, err = LogLevel("INFO") 53 if err != nil { 54 t.Errorf("LogLevel(INFO) failed") 55 } 56 if level != LogInfo { 57 t.Errorf("LogLevel(INFO) did not return LogInfo") 58 } 59 level, err = LogLevel("Notice") 60 if err != nil { 61 t.Errorf("LogLevel(Notice) failed") 62 } 63 if level != LogNotice { 64 t.Errorf("LogLevel(Notice) did not return LogNotice") 65 } 66 level, err = LogLevel("WaRnInG") 67 if err != nil { 68 t.Errorf("LogLevel(WaRnInG) failed") 69 } 70 if level != LogWarning { 71 t.Errorf("LogLevel(WaRnInG) did not return LogWarning") 72 } 73 level, err = LogLevel("ERRor") 74 if err != nil { 75 t.Errorf("LogLevel(ERRor) failed") 76 } 77 if level != LogError { 78 t.Errorf("LogLevel(ERRor) did not return LogError") 79 } 80 level, err = LogLevel("critiCAL") 81 if err != nil { 82 t.Errorf("LogLevel(critiCAL) failed") 83 } 84 if level != LogCritical { 85 t.Errorf("LogLevel(critiCAL) did not return LogCritical") 86 } 87 level, err = LogLevel("foo") 88 if err == nil { 89 t.Errorf("LogLevel(foo) did not fail") 90 } 91 if level != LogError { 92 t.Errorf("LogLevel(foo) did not return LogError") 93 } 94 } 95 96 // TestChaincodeLogging tests the logging APIs for chaincodes. 97 func TestChaincodeLogging(t *testing.T) { 98 99 // From start() - We can't call start() from this test 100 format := logging.MustStringFormatter("%{time:15:04:05.000} [%{module}] %{level:.4s} : %{message}") 101 backend := logging.NewLogBackend(os.Stderr, "", 0) 102 backendFormatter := logging.NewBackendFormatter(backend, format) 103 logging.SetBackend(backendFormatter).SetLevel(logging.Level(shimLoggingLevel), "shim") 104 105 foo := NewLogger("foo") 106 bar := NewLogger("bar") 107 108 foo.Debugf("Foo is debugging: %d", 10) 109 bar.Infof("Bar is informational? %s.", "Yes") 110 foo.Noticef("NOTE NOTE NOTE") 111 bar.Warningf("Danger, Danger %s %s", "Will", "Robinson!") 112 foo.Errorf("I'm sorry Dave, I'm afraid I can't do that.") 113 bar.Criticalf("PI is not equal to 3.14, we computed it as %.2f", 4.13) 114 115 bar.Debug("Foo is debugging:", 10) 116 foo.Info("Bar is informational?", "Yes.") 117 bar.Notice("NOTE NOTE NOTE") 118 foo.Warning("Danger, Danger", "Will", "Robinson!") 119 bar.Error("I'm sorry Dave, I'm afraid I can't do that.") 120 foo.Critical("PI is not equal to", 3.14, ", we computed it as", 4.13) 121 122 foo.SetLevel(LogWarning) 123 if foo.IsEnabledFor(LogDebug) { 124 t.Errorf("'foo' should not be enabled for LogDebug") 125 } 126 if !foo.IsEnabledFor(LogCritical) { 127 t.Errorf("'foo' should be enabled for LogCritical") 128 } 129 bar.SetLevel(LogCritical) 130 if bar.IsEnabledFor(LogDebug) { 131 t.Errorf("'bar' should not be enabled for LogDebug") 132 } 133 if !bar.IsEnabledFor(LogCritical) { 134 t.Errorf("'bar' should be enabled for LogCritical") 135 } 136 } 137 138 func TestNilEventName(t *testing.T) { 139 stub := ChaincodeStub{} 140 if err := stub.SetEvent("", []byte("event payload")); err == nil { 141 t.Error("Event name can not be nil string.") 142 } 143 144 }