Skip to content

Commit

Permalink
test(testing): service_test.go - broken pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephKav committed Apr 21, 2024
1 parent 2ca97a9 commit ffcac8c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
20 changes: 11 additions & 9 deletions service/latest_version/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (l *Lookup) queryMetrics(err error) {
}
}

func (l *Lookup) httpRequest(logFrom *util.LogFrom) (rawBody []byte, err error) {
func (l *Lookup) httpRequest(logFrom *util.LogFrom) (rawBodyPtr *[]byte, err error) {
customTransport := &http.Transport{}
// HTTPS insecure skip verify.
if l.GetAllowInvalidCerts() {
Expand Down Expand Up @@ -222,7 +222,9 @@ func (l *Lookup) httpRequest(logFrom *util.LogFrom) (rawBody []byte, err error)

// Read the response body.
defer resp.Body.Close()
var rawBody []byte
rawBody, err = io.ReadAll(resp.Body)
rawBodyPtr = &rawBody
jLog.Error(err, logFrom, err != nil)
if l.Type == "github" && err == nil {
// 200 - Resource has changed
Expand All @@ -237,7 +239,7 @@ func (l *Lookup) httpRequest(logFrom *util.LogFrom) (rawBody []byte, err error)
l.GitHubData.SetTagFallback()
if l.GitHubData.TagFallback() {
jLog.Verbose(fmt.Sprintf("/releases gave %v, trying /tags", string(rawBody)), logFrom, true)
rawBody, err = l.httpRequest(logFrom)
rawBodyPtr, err = l.httpRequest(logFrom)
}
// Has tags/releases
} else {
Expand All @@ -253,7 +255,7 @@ func (l *Lookup) httpRequest(logFrom *util.LogFrom) (rawBody []byte, err error)
l.GitHubData.SetTagFallback()
if l.GitHubData.TagFallback() {
jLog.Verbose("no tags found on /releases, trying /tags", logFrom, true)
rawBody, err = l.httpRequest(logFrom)
rawBodyPtr, err = l.httpRequest(logFrom)
}
}
}
Expand All @@ -264,14 +266,14 @@ func (l *Lookup) httpRequest(logFrom *util.LogFrom) (rawBody []byte, err error)
// GetVersions will filter out releases from rawBody that are preReleases (if not wanted) and will sort releases if
// semantic versioning is wanted
func (l *Lookup) GetVersions(
rawBody []byte,
rawBody *[]byte,
logFrom *util.LogFrom,
) (filteredReleases []github_types.Release, err error) {
var releases []github_types.Release
body := string(rawBody)
body := string(*rawBody)
// GitHub service.
if l.Type == "github" {
releases, err = l.checkGitHubReleasesBody(&rawBody, logFrom)
releases, err = l.checkGitHubReleasesBody(rawBody, logFrom)
if err != nil {
return
}
Expand Down Expand Up @@ -299,10 +301,10 @@ func (l *Lookup) GetVersions(
}

// GetVersion will return the latest version from rawBody matching the URLCommands and Regex requirements
func (l *Lookup) GetVersion(rawBody []byte, logFrom *util.LogFrom) (version string, err error) {
func (l *Lookup) GetVersion(rawBody *[]byte, logFrom *util.LogFrom) (version string, err error) {
var filteredReleases []github_types.Release
// rawBody length = 0 if GitHub ETag is unchanged
if len(rawBody) != 0 {
if len(*rawBody) != 0 {
filteredReleases, err = l.GetVersions(rawBody, logFrom)
if err != nil {
return
Expand Down Expand Up @@ -337,7 +339,7 @@ func (l *Lookup) GetVersion(rawBody []byte, logFrom *util.LogFrom) (version stri
body = filteredReleases[i].Assets
// Web service
} else {
body = string(rawBody)
body = string(*rawBody)
}
// If the Content doesn't match the provided RegEx
if err = l.Require.RegexCheckContent(version, body, logFrom); err != nil {
Expand Down
44 changes: 24 additions & 20 deletions testing/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ func ServiceTest(
if *flag == "" {
return
}
logFrom := &util.LogFrom{Primary: "Testing", Secondary: *flag}

// Log what we are testing
logFrom := &util.LogFrom{Primary: "Testing", Secondary: *flag}
log.Info(
"",
logFrom,
true,
)
service := cfg.Service[*flag]

if service == nil {
// Check if service exists
if !util.Contains(cfg.Order, *flag) {
log.Fatal(
fmt.Sprintf(
"Service %q could not be found in config.service\nDid you mean one of these?\n - %s",
Expand All @@ -53,24 +54,26 @@ func ServiceTest(
)
}

if service != nil {
_, err := service.LatestVersion.Query(false, logFrom)
if err != nil {
helpMsg := ""
if service.LatestVersion.Type == "url" && strings.Count(service.LatestVersion.URL, "/") == 1 && !strings.HasPrefix(service.LatestVersion.URL, "http") {
helpMsg = "This URL looks to be a GitHub repo, but the service's type is url, not github. Try using the github service type.\n"
}
log.Error(
fmt.Sprintf(
"No version matching the conditions specified could be found for %q at %q\n%s",
*flag,
service.LatestVersion.ServiceURL(true),
helpMsg,
),
logFrom,
true,
)
// Service we are testing
service := cfg.Service[*flag]

// LatestVersion
_, err := service.LatestVersion.Query(false, logFrom)
if err != nil {
helpMsg := ""
if service.LatestVersion.Type == "url" && strings.Count(service.LatestVersion.URL, "/") == 1 && !strings.HasPrefix(service.LatestVersion.URL, "http") {
helpMsg = "This URL looks to be a GitHub repo, but the service's type is url, not github. Try using the github service type.\n"
}
log.Error(
fmt.Sprintf(
"No version matching the conditions specified could be found for %q at %q\n%s",
*flag,
service.LatestVersion.ServiceURL(true),
helpMsg,
),
logFrom,
true,
)
}

// DeployedVersionLookup
Expand All @@ -85,6 +88,7 @@ func ServiceTest(
err == nil,
)
}

if !log.Testing {
os.Exit(0)
}
Expand Down

0 comments on commit ffcac8c

Please sign in to comment.