Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test for online connection #222

Merged
merged 14 commits into from
Nov 13, 2023
62 changes: 62 additions & 0 deletions cmd/commands/connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* SPDX-License-Identifier: Apache-2.0 */
/* Copyright Contributors to the cpackget project. */

package commands

import (
"github.com/open-cmsis-pack/cpackget/cmd/installer"
"github.com/open-cmsis-pack/cpackget/cmd/utils"
"github.com/spf13/cobra"
)

var connectionCmdFlags struct {
// downloadPdscFiles forces all pdsc files from the public index to be downloaded
downloadPdscFiles bool

// Reports encoded progress for files and download when used by other tools
encodedProgress bool

// skipTouch does not touch pack.idx after adding
skipTouch bool

// check connection status
checkConnection bool
}

var ConnectionCmd = &cobra.Command{
Use: "connection [<url>]",
Short: "Check online connection to default or given URL",
Long: `Checks if the given or default url is accessible
The url is optional. Ex "cpackget connection https://www.keil.com/pack"`,
Args: cobra.MinimumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
utils.SetEncodedProgress(connectionCmdFlags.encodedProgress)
utils.SetSkipTouch(connectionCmdFlags.skipTouch)

var indexPath string
if len(args) > 0 {
indexPath = args[0]
}
createPackRoot = false
var err error

if indexPath == "" { // try to fetch from environment
err = configureInstaller(cmd, args)
if err != nil {
return err
}
}

indexPath, err = installer.GetIndexPath(indexPath)
if err != nil {
return err
}

err = utils.CheckConnection(indexPath, viper.GetInt("timeout"))
return err
},
}

func init() {
ConnectionCmd.Flags().BoolVarP(&connectionCmdFlags.encodedProgress, "encoded-progress", "E", false, "Reports encoded progress for files and download when used by other tools")
}
60 changes: 60 additions & 0 deletions cmd/commands/connection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* SPDX-License-Identifier: Apache-2.0 */
/* Copyright Contributors to the cpackget project. */

package commands_test

import (
"errors"
"testing"
)

var (
urlPath string = "https://www.keil.com"
wrongURLPath string = "https://www.keilll.com"
)

var connectionCmdTests = []TestCase{
{
name: "test help command",
args: []string{"help", "connection"},
expectedErr: nil,
},
{
name: "test checking connection",
args: []string{"connection", urlPath},
expectedErr: nil,
},
{
name: "test checking invalid url",
args: []string{"connection", wrongURLPath},
expectedErr: errors.New("remote server is offline or cannot be reached"),
},

{ // set up environment for next test
name: "test checking default connection",
args: []string{"init"},
noCleanup: true,
setUpFunc: func(t *TestCase) {
server := NewServer()
t.args = append(t.args, server.URL()+"index.pidx")
server.AddRoute("index.pidx", []byte(`<?xml version="1.0" encoding="UTF-8" ?>
<index schemaVersion="1.1.0" xs:noNamespaceSchemaLocation="PackIndex.xsd" xmlns:xs="https://www.w3.org/2001/XMLSchema-instance">
<vendor>TheVendor</vendor>
<url>https://www.keil.com/</url>
<timestamp>2021-10-17T12:21:59.1747971+00:00</timestamp>
<pindex>
<pdsc url="https://www.keil.com" vendor="Keil" name="PackName" version="1.2.3" />
</pindex>
</index>`))
},
},
{
name: "test checking default connection",
args: []string{"connection"},
expectedErr: nil,
},
}

func TestConnectionCmd(t *testing.T) {
runTests(t, connectionCmdTests)
}
9 changes: 6 additions & 3 deletions cmd/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var AllCommands = []*cobra.Command{
ChecksumVerifyCmd,
SignatureCreateCmd,
SignatureVerifyCmd,
ConnectionCmd,
}

// createPackRoot is a flag that determines if the pack root should be created or not
Expand Down Expand Up @@ -68,10 +69,12 @@ func configureInstaller(cmd *cobra.Command, args []string) error {
}

targetPackRoot := viper.GetString("pack-root")
checkConnection := viper.GetBool("check-connection")

if targetPackRoot == installer.GetDefaultCmsisPackRoot() {
// If using the default pack root path and the public index is not found,
// initialize it
if !utils.FileExists(filepath.Join(targetPackRoot, ".Web", "index.pidx")) {
if !checkConnection && !utils.FileExists(filepath.Join(targetPackRoot, ".Web", "index.pidx")) {
err := installer.SetPackRoot(targetPackRoot, true)
if err != nil {
return err
Expand Down Expand Up @@ -110,10 +113,10 @@ var flags struct {
}

var Version string
var CopyRight string
var Copyright string

func printVersionAndLicense(file io.Writer) {
fmt.Fprintf(file, "cpackget version %v %s\n", strings.ReplaceAll(Version, "v", ""), CopyRight)
fmt.Fprintf(file, "cpackget version %v %s\n", strings.ReplaceAll(Version, "v", ""), Copyright)
}

// UsageTemplate returns usage template for the command.
Expand Down
7 changes: 5 additions & 2 deletions cmd/commands/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type TestCase struct {
validationFunc func(t *testing.T)
assert *assert.Assertions
env map[string]string
noCleanup bool
}

type Server struct {
Expand Down Expand Up @@ -161,8 +162,10 @@ func runTests(t *testing.T, tests []TestCase) {
}

defer func() {
utils.UnsetReadOnlyR(localTestingDir)
os.RemoveAll(localTestingDir)
if !test.noCleanup {
utils.UnsetReadOnlyR(localTestingDir)
os.RemoveAll(localTestingDir)
}
}()

cmd := commands.NewCli()
Expand Down
33 changes: 29 additions & 4 deletions cmd/installer/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/open-cmsis-pack/cpackget/cmd/utils"
"github.com/open-cmsis-pack/cpackget/cmd/xml"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"golang.org/x/mod/semver"
"golang.org/x/sync/semaphore"
)
Expand Down Expand Up @@ -422,6 +423,26 @@ func UpdateInstalledPDSCFiles(pidxXML *xml.PidxXML, concurrency int, timeout int
return nil
}

func GetIndexPath(indexPath string) (string, error) {
if indexPath == "" {
indexPath = strings.TrimSuffix(Installation.PublicIndexXML.URL, "/")
}

if !utils.GetEncodedProgress() {
log.Infof("Using path: \"%v\"", indexPath)
}

var err error

if strings.HasPrefix(indexPath, "http://") || strings.HasPrefix(indexPath, "https://") {
if !strings.HasPrefix(indexPath, "https://") {
log.Warnf("Non-HTTPS url: \"%s\"", indexPath)
}
}

return indexPath, err
}

// UpdatePublicIndex receives a index path and place it under .Web/index.pidx.
func UpdatePublicIndex(indexPath string, overwrite bool, sparse bool, downloadPdsc bool, downloadRemainingPdscFiles bool, concurrency int, timeout int) error {
// TODO: Remove overwrite when cpackget v1 gets released
Expand Down Expand Up @@ -843,10 +864,14 @@ func SetPackRoot(packRoot string, create bool) error {
if !utils.DirExists(packRoot) && !create {
return errs.ErrPackRootDoesNotExist
}
if packRoot == GetDefaultCmsisPackRoot() {
log.Infof("Using pack root: \"%v\" (default mode - no specific CMSIS_PACK_ROOT chosen)", packRoot)
} else {
log.Infof("Using pack root: \"%v\"", packRoot)

checkConnection := viper.GetBool("check-connection")
if checkConnection && !utils.GetEncodedProgress() {
if packRoot == GetDefaultCmsisPackRoot() {
log.Infof("Using pack root: \"%v\" (default mode - no specific CMSIS_PACK_ROOT chosen)", packRoot)
} else {
log.Infof("Using pack root: \"%v\"", packRoot)
}
}

Installation = &PacksInstallationType{
Expand Down
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
start := time.Now()

commands.Version = version
commands.CopyRight = copyRight
commands.Copyright = copyRight
cmd := commands.NewCli()
err := cmd.Execute()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/utils/encodedProgress.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (p *EncodedProgress) Write(bs []byte) (int, error) {
* C: Currently processed bytes or numbers of files
* J: Total number of files beeing processed
* L: License file follows
* O: Online connection Status [offline|online]
*/
func (p *EncodedProgress) Print() {
newPercent := int(float64(p.current) / float64(p.total) * 100)
Expand Down
30 changes: 30 additions & 0 deletions cmd/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,36 @@ func DownloadFile(URL string, timeout int) (string, error) {
return filePath, err
}

func CheckConnection(url string, timeOut int) error {
timeout := time.Duration(timeOut) * time.Second
client := http.Client{
Timeout: timeout,
}
resp, err := client.Get(url)
connStatus := "offline"
if err != nil {
if !GetEncodedProgress() {
log.Info(err)
}
} else {
connStatus = "online"
if !GetEncodedProgress() {
text := fmt.Sprintf("Respond: %v:%v (%v)", resp.StatusCode, resp.Status, connStatus)
log.Info(text)
}
}

if GetEncodedProgress() {
log.Infof("[O:%v]", connStatus)
}

if connStatus == "offline" {
return errors.New("remote server is offline or cannot be reached")
}

return nil
}

// FileExists checks if filePath is an actual file in the local file system
func FileExists(filePath string) bool {
info, err := os.Stat(filePath)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/open-cmsis-pack/cpackget

go 1.20
go 1.21

require (
github.com/ProtonMail/gopenpgp/v2 v2.4.10
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/gabriel-vasile/mimetype v1.1.1/go.mod h1:6CDPel/o/3/s4+bp6kIbsWATq8pmgOisOPG40CJa6To=
Expand Down Expand Up @@ -116,6 +117,7 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
Expand Down Expand Up @@ -153,9 +155,11 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lu4p/cat v0.1.5 h1:s51Bp/ns3u6n+hjjL2F77ySY6j/GD5SJG/t6Ok4Y1S0=
github.com/lu4p/cat v0.1.5/go.mod h1:G3YRyjSvBipqMBRZ2uLf1oRL3/eGGmuZf96m95Y4jRQ=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
Expand Down Expand Up @@ -185,6 +189,7 @@ github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/schollz/progressbar/v3 v3.12.2 h1:yLqqqpQNMxGxHY8uEshRihaHWwa0rf0yb7/Zrpgq2C0=
github.com/schollz/progressbar/v3 v3.12.2/go.mod h1:HFJYIYQQJX32UJdyoigUl19xoV6aMwZt6iX/C30RWfg=
Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ coverage-report: test
go tool cover -html=cover.out

coverage-check: test
@echo Checking if test coverage is atleast 85%
@echo Checking if test coverage is atleast 84%
test `go tool cover -func cover.out | tail -1 | awk '{print ($$3 + 0)*10}'` -ge 840

test-public-index:
Expand Down
Loading