Add agentport flag check

This commit is contained in:
memoryliu 2020-07-06 21:20:39 +08:00
parent f4d07c6a93
commit ac34f416da
8 changed files with 196 additions and 19 deletions

3
.gitignore vendored
View File

@ -9,3 +9,6 @@ goc
# the temp file to save service address # the temp file to save service address
_svrs_address.txt _svrs_address.txt
# other
*.iml

View File

@ -71,7 +71,7 @@ func runBuild(args []string, wd string) {
defer gocBuild.Clean() defer gocBuild.Clean()
// doCover with original buildFlags, with new GOPATH( tmp:original ) // doCover with original buildFlags, with new GOPATH( tmp:original )
// in the tmp directory // in the tmp directory
cover.Execute(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir, mode, agentPort, center) cover.Execute(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir, coverMode.String(), agentPort.String(), center)
// do install in the temporary directory // do install in the temporary directory
err = gocBuild.Build() err = gocBuild.Build()
if err != nil { if err != nil {

View File

@ -1,6 +1,25 @@
/*
Copyright 2020 Qiniu Cloud (qiniu.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd package cmd
import ( import (
"fmt"
"net"
"github.com/spf13/pflag" "github.com/spf13/pflag"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -8,8 +27,7 @@ import (
var ( var (
target string target string
center string center string
mode string agentPort AgentPort
agentPort string
debugGoc bool debugGoc bool
buildFlags string buildFlags string
@ -17,6 +35,10 @@ var (
goRunArguments string goRunArguments string
) )
var coverMode = CoverMode{
mode: "count",
}
// addBasicFlags adds a // addBasicFlags adds a
func addBasicFlags(cmdset *pflag.FlagSet) { func addBasicFlags(cmdset *pflag.FlagSet) {
cmdset.StringVar(&center, "center", "http://127.0.0.1:7777", "cover profile host center") cmdset.StringVar(&center, "center", "http://127.0.0.1:7777", "cover profile host center")
@ -26,8 +48,8 @@ func addBasicFlags(cmdset *pflag.FlagSet) {
func addCommonFlags(cmdset *pflag.FlagSet) { func addCommonFlags(cmdset *pflag.FlagSet) {
addBasicFlags(cmdset) addBasicFlags(cmdset)
cmdset.StringVar(&mode, "mode", "count", "coverage mode: set, count, atomic") cmdset.Var(&coverMode, "mode", "coverage mode: set, count, atomic")
cmdset.StringVar(&agentPort, "agentport", "", "specify fixed port for registered service communicate with goc server. if not provided, using a random one") cmdset.Var(&agentPort, "agentport", "a fixed port such as :8100 for registered service communicate with goc server. if not provided, using a random one")
cmdset.StringVar(&buildFlags, "buildflags", "", "specify the build flags") cmdset.StringVar(&buildFlags, "buildflags", "", "specify the build flags")
// bind to viper // bind to viper
viper.BindPFlags(cmdset) viper.BindPFlags(cmdset)
@ -46,3 +68,54 @@ func addRunFlags(cmdset *pflag.FlagSet) {
// bind to viper // bind to viper
viper.BindPFlags(cmdset) viper.BindPFlags(cmdset)
} }
// add Cover Mode check
type CoverMode struct {
mode string
}
func (m *CoverMode) String() string {
return m.mode
}
func (m *CoverMode) Set(v string) error {
if v == "" {
m.mode = "count"
return nil
}
if v != "set" && v != "count" && v != "atomic" {
return fmt.Errorf("unknown mode")
}
m.mode = v
return nil
}
func (m *CoverMode) Type() string {
return "string"
}
// add agentPort check
type AgentPort struct {
port string
}
func (agent *AgentPort) String() string {
return agent.port
}
func (agent *AgentPort) Set(v string) error {
if v == "" {
agent.port = ""
return nil
}
_, _, err := net.SplitHostPort(v)
if err != nil {
return err
}
agent.port = v
return nil
}
func (agent *AgentPort) Type() string {
return "string"
}

110
cmd/commonflags_test.go Normal file
View File

@ -0,0 +1,110 @@
/*
Copyright 2020 Qiniu Cloud (qiniu.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCoverModeFlag(t *testing.T) {
var tcs = []struct {
value string
expectedValue interface{}
err interface{}
}{
{
value: "",
expectedValue: "count",
err: nil,
},
{
value: "set",
expectedValue: "set",
err: nil,
},
{
value: "count",
expectedValue: "count",
err: nil,
},
{
value: "atomic",
expectedValue: "atomic",
err: nil,
},
{
value: "xxxxx",
expectedValue: "",
err: errors.New("unknown mode"),
},
{
value: "123333",
expectedValue: "",
err: errors.New("unknown mode"),
},
}
for _, tc := range tcs {
mode := &CoverMode{}
err := mode.Set(tc.value)
actual := mode.String()
assert.Equal(t, actual, tc.expectedValue, fmt.Sprintf("check mode flag value failed, expected %s, got %s", tc.expectedValue, actual))
assert.Equal(t, err, tc.err, fmt.Sprintf("check mode flag error, expected %s, got %s", tc.err, err))
}
}
func TestAgentPortFlag(t *testing.T) {
var tcs = []struct {
value string
expectedValue interface{}
isErr bool
}{
{
value: "",
expectedValue: "",
isErr: false,
},
{
value: ":8888",
expectedValue: ":8888",
isErr: false,
},
{
value: "8888",
expectedValue: "",
isErr: true,
},
{
value: "::8888",
expectedValue: "",
isErr: true,
},
}
for _, tc := range tcs {
agent := &AgentPort{}
err := agent.Set(tc.value)
if tc.isErr {
assert.NotEqual(t, nil, err, fmt.Sprintf("check agentport flag error, expected %v, got %v", nil, err))
} else {
actual := agent.String()
assert.Equal(t, tc.expectedValue, actual, fmt.Sprintf("check agentport flag value failed, expected %s, got %s", tc.expectedValue, actual))
}
}
}

View File

@ -18,7 +18,6 @@ package cmd
import ( import (
"github.com/qiniu/goc/pkg/cover" "github.com/qiniu/goc/pkg/cover"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -41,14 +40,7 @@ goc cover --center=http://127.0.0.1:7777 --target=/path/to/target --mode=atomic
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var buildFlags string var buildFlags string
buildFlags = viper.GetString("buildflags") buildFlags = viper.GetString("buildflags")
if mode == "" { cover.Execute(buildFlags, "", target, coverMode.String(), agentPort.String(), center)
log.Fatalf("Error: flag needs an argument: -mode %v", mode)
}
if mode != "set" && mode != "count" && mode != "atomic" {
log.Fatalf("unknown -mode %v", mode)
}
cover.Execute(buildFlags, "", target, mode, agentPort, center)
}, },
} }

View File

@ -64,7 +64,7 @@ func runInstall(args []string, wd string) {
defer gocBuild.Clean() defer gocBuild.Clean()
// doCover with original buildFlags, with new GOPATH( tmp:original ) // doCover with original buildFlags, with new GOPATH( tmp:original )
// in the tmp directory // in the tmp directory
cover.Execute(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir, mode, agentPort, center) cover.Execute(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir, coverMode.String(), agentPort.String(), center)
// do install in the temporary directory // do install in the temporary directory
err = gocBuild.Install() err = gocBuild.Install()
if err != nil { if err != nil {

View File

@ -60,7 +60,7 @@ goc run . [--buildflags] [--exec] [--arguments]
fmt.Printf("[goc] goc server started: %s \n", gocServer) fmt.Printf("[goc] goc server started: %s \n", gocServer)
// execute covers for the target source with original buildFlags and new GOPATH( tmp:original ) // execute covers for the target source with original buildFlags and new GOPATH( tmp:original )
cover.Execute(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir, mode, "", gocServer) cover.Execute(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir, coverMode.String(), "", gocServer)
if err := gocBuild.Run(); err != nil { if err := gocBuild.Run(); err != nil {
log.Fatalf("Fail to run: %v", err) log.Fatalf("Fail to run: %v", err)

View File

@ -126,7 +126,6 @@ func Execute(args, newGopath, target, mode, agentPort, center string) error {
log.Errorf("Target directory %s not exist", target) log.Errorf("Target directory %s not exist", target)
return ErrCoverPkgFailed return ErrCoverPkgFailed
} }
listArgs := []string{"-json"} listArgs := []string{"-json"}
if len(args) != 0 { if len(args) != 0 {
listArgs = append(listArgs, args) listArgs = append(listArgs, args)
@ -302,7 +301,7 @@ func AddCounters(pkg *Package, mode, newgopath string) (*PackageCover, error) {
cmd := buildCoverCmd(file, coverVar, pkg, mode, newgopath) cmd := buildCoverCmd(file, coverVar, pkg, mode, newgopath)
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return nil, fmt.Errorf("execute go tool cover -mode=atomic -var %s -o %s/%s failed, err: %v, out: %s", coverVar.Var, pkg.Dir, file, err, string(out)) return nil, fmt.Errorf("execute go tool cover -mode=%s -var %s -o %s/%s failed, err: %v, out: %s", mode, coverVar.Var, pkg.Dir, file, err, string(out))
} }
} }