I have a groovy pipeline script:
stage("Test") {
str="[\"asd1\", \"asd2\"]"
def tagNames = str.tokenize(',[]').collect { it as String }
echo "${tagNames.getClass()}"
echo "${tagNames.size}"
}
But on the output I see error "No such field found: field java.lang.String size":
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
class java.util.ArrayList
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
No such field found: field java.lang.String size. Administrators can decide whether to approve or reject this signature.
[Pipeline] End of Pipeline
What I'm doing wrong?
Edit: I have "No pending signature approvals."
Using
size()
instead of
size
does the trick.
Related
I'm trying to include godog in a go project in order to test some APIs.
This is the structure
Project Folder
...
...
src
cmd
features
ready.feature
main.go
main_test.go
The webserver starts in the main() function contained in a file named main.go under the cmd folder. So I have added under the cmd folder a new features folder with a ready.feature file:
Feature: get ready
In order to know if microservice is ready
As an API user
I need to be able to check the ready
Scenario: should get ready
When I send "GET" request to "/ready"
Then the response code should be 200
And the response should match json:
"""
{
service is ready
}
"""
Now, after the execution of this command:
godog run ready.feature
I get :
func iSendRequestTo(arg1, arg2 string) error {
return godog.ErrPending
}
func theResponseCodeShouldBe(arg1 int) error {
return godog.ErrPending
}
func theResponseShouldMatchJson(arg1 *godog.DocString) error {
return godog.ErrPending
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(`^I send "([^"]*)" request to "([^"]*)"$`, iSendRequestTo)
ctx.Step(`^the response code should be (\d+)$`, theResponseCodeShouldBe)
ctx.Step(`^the response should match json:$`, theResponseShouldMatchJson)
}
So I've created the main_test.go file under the cmd folder with the content suggested by godog and tried to run godog (in order to run the test) with the command "godog run" but I get every time (and yes, I've reworked the main_test.go file) this error:
failed to compile testmain package: exit status 2 - output: /tmp/go-build2631598204/b001/_testmain.go:5:8: could not import project/src/cmd (open : no such file or directory)
Could you help me, please ?
I have 2 files in the same package.
# my_package1/my_file1.go
func myFunc1() {
//....
}
# my_package1/my_file1_test.go
type MyPackageSuite struct {
suite.Suite
}
func (s *MyPackageSuite) MyTest1() {
//...............
res1 := myFunc1()
//...............
}
I run a test go test my_package1/my_file1_test.go -v and it returns undefined: myFunc1
But they're in the same package. Why the error? How to fix it? Making the method public isn't what I'm looking for.
Update1:
$ ls webhook
doc.go webhook.go webhook_test.go
Then
$ go test webhook
can't load package: package webhook: malformed module path "webhook": missing dot in first path element
$ go test webhook/webhook
can't load package: package webhook/webhook: malformed module path "webhook/webhook": missing dot in first path element
$ go test webhook/webhook.go
? command-line-arguments [no test files]
$ go test webhook/webhook_test.go
# command-line-arguments [command-line-arguments.test]
webhook/webhook_test.go: undefined: myFunc1
FAIL command-line-arguments [build failed]
FAIL
Try go test ./my_package1 to force the compilation of the all package.
Or at least a go build first.
The idea is to make sure myFunc1() was compiled before executing the test file.
The "malformed module path" "missing dot in first path element" suggests a go mod file is missing:
cd /path/to/project
go mod init project
go test ./webhook
When I try to modify the example described in this hyperledger example I get some error when adding this external library in order to get the History of the chaincode state.
Why does that happen?
I add the library with govendor, but when I run this command:
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin#org1.example.com/msp" cli peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n $CC_NAME -l "$LANGUAGE" -v 1.0 -c $INIT_STR -P "OR ('Org1MSP.member','Org2MSP.member')"
I get this error:
Error: Error endorsing chaincode:
rpc error: code = Unknown desc = error starting container: Failed to generate platform-specific docker build: Error returned from build: 2 "# firstExample
chaincode/input/src/firstExample/firstStep.go:104:11: cannot assign *"github.com/hyperledger/fabric/protos/ledger/queryresult".KeyModification to kM (type *"firstExample/vendor/github.com/hyperledger/fabric/protos/ledger/queryresult".KeyModification) in multiple assignment
chaincode/input/src/firstExample/firstStep.go:146:11: cannot assign *"github.com/hyperledger/fabric/protos/ledger/queryresult".KeyModification to kM (type *"firstExample/vendor/github.com/hyperledger/fabric/protos/ledger/queryresult".KeyModification) in multiple assignment
chaincode/input/src/firstExample/firstStep.go:156:11: cannot assign *"github.com/hyperledger/fabric/protos/ledger/queryresult".KeyModification to kM (type *"firstExample/vendor/github.com/hyperledger/fabric/protos/ledger/queryresult".KeyModification) in multiple assignment
I have some troubles with this. I'm sure that the library is imported because if I build the chaincode written in go with the command:
go build
I get no errors.
Please help me!
It's very hard to guess without seeing actual code which cause the compilation error, while it seems that you are not taking care of the second parameter which returned by HistoryQueryIteratorInterface#Next() API or even ChaincodeStubInterface#GetHistoryForKey(). Please see example of how to use those APIs correctly:
// GetPreviousValue reads previous value of given key
func (pm *personManagement) GetPreviousValue(params []string, stub shim.ChaincodeStubInterface) peer.Response {
historyIer, err := stub.GetHistoryForKey(params[0])
if err != nil {
errMsg := fmt.Sprintf("[ERROR] cannot retrieve history of key <%s>, due to %s", params[0], err)
fmt.Println(errMsg)
return shim.Error(errMsg)
}
if historyIer.HasNext() {
modification, err := historyIer.Next()
if err != nil {
errMsg := fmt.Sprintf("[ERROR] cannot read key record modification, key <%s>, due to %s", params[0], err)
fmt.Println(errMsg)
return shim.Error(errMsg)
}
fmt.Println("Returning information about", string(modification.Value))
return shim.Success(modification.Value)
}
fmt.Printf("No history found for key %s\n", params[0])
return shim.Success([]byte(fmt.Sprintf("No history for key %s", params[0])))
}
NOTE: Please pay attention that historyIer.Next() returns history value and the error.
I make a makefile that executes go test -cover. Is it possible to fail the make unit_tests command if coverage is below X? How would I do that?
You can use TestMain in your test to do that. TestMain can act as a custom entry point to tests, and then you can invoke testing.Coverage() to get access to the coverage stats.
So for example, if you want to fail at anything below 80%, you could add this to one of your package's test files:
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
rc := m.Run()
// rc 0 means we've passed,
// and CoverMode will be non empty if run with -cover
if rc == 0 && testing.CoverMode() != "" {
c := testing.Coverage()
if c < 0.8 {
fmt.Println("Tests passed but coverage failed at", c)
rc = -1
}
}
os.Exit(rc)
}
Then go test -cover will call this entry point and you'll fail:
PASS
coverage: 63.0% of statements
Tests passed but coverage failed at 0.5862068965517241
exit status 255
FAIL github.com/xxxx/xxx 0.026s
Notice that the number that testing.Coverage() returns is lower than what the test reports. I've looked at the code and the function calculates its coverage differently than the test's internal reporting. I'm not sure which is more "correct".
I am aware that the currently supported method for invoking a task from another task is to use dependsOn or finalizedBy, but I run into an issue with this.
I have a task, taskA, that is usable on it's own. I have another task, taskB, which, when called, will depend upon taskA. The problem is that taskB has additional conditions that require the task be skipped if they fail. Here is the workflow I am going for:
$ gradle taskA
:taskA
BUILD SUCCESSFUL
$ gradle taskB
checking condition 1... PASS
checking condition 2... PASS
:taskA
:taskB
BUILD SUCCESSFUL
$ gradle taskB
checking condition 1... PASS
checking condition 2... FAIL
:taskA SKIPPED
:taskB SKIPPED
BUILD SUCCESSFUL
If called directly, or as a doFirst or dependsOn or something from a different task, taskA should execute regardless of the conditions. But if taskB is called, the conditions must pass before taskA is executed. Here's what I've tried:
project.tasks.create(name: "taskB", type: MyTask, dependsOn: "taskA")
project.taskB{
onlyIf {
if (!conditionA()){
return false
}
if (!conditionB()){
return false
}
return true
}
}
The problem here is that taskA will execute before the onlyIf is checked on taskB, so even if a condition fails, taskA will execute.
How can I accomplish this?
It seems that it can be configured no sooner than after task's graph has been resolved. At the earlier stages all conditions will be evaluated on configuration phase which is too early. Have a look at this:
task a {
doLast {
println 'a'
}
}
task b {
dependsOn a
doLast {
println 'b'
}
}
gradle.taskGraph.whenReady { graph ->
if (graph.hasTask(b)) {
a.enabled = b.enabled = check1() && check2()
}
}
boolean check1() {
def ok = project.hasProperty('c')
println "check 1: ${ok ? 'PASS' : 'FAIL'}"
ok
}
boolean check2() {
def ok = project.hasProperty('d')
println "check 2: ${ok ? 'PASS' : 'FAIL'}"
ok
}
And the output:
~/tutorial/stackoverflow/40850083/ [master] gradle a
:a
a
BUILD SUCCESSFUL
Total time: 1.728 secs
~/tutorial/stackoverflow/40850083/ [master] gradle b
check 1: FAIL
:a SKIPPED
:b SKIPPED
BUILD SUCCESSFUL
Total time: 1.739 secs
~/tutorial/stackoverflow/40850083/ [master] gradle b -Pc
check 1: PASS
check 2: FAIL
:a SKIPPED
:b SKIPPED
BUILD SUCCESSFUL
Total time: 1.714 secs
~/tutorial/stackoverflow/40850083/ [master] gradle b -Pc -Pd
check 1: PASS
check 2: PASS
:a
a
:b
b
BUILD SUCCESSFUL
Total time: 1.745 secs
I know it's not recommended by most people, but I did it by actually executing a task, thusly:
task a {
doLast {
println 'a'
}
}
task b {
doLast {
a.execute()
println 'b'
}
outputs.upToDateWhen {
conditionA() && conditionB()
}
}