You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2022/05/21 23:26:34 UTC

[GitHub] [trafficcontrol] ericholguin opened a new pull request, #6852: Move duplicate rand functionality to test rand

ericholguin opened a new pull request, #6852:
URL: https://github.com/apache/trafficcontrol/pull/6852

   <!--
   Thank you for contributing! Please be sure to read our contribution guidelines: https://github.com/apache/trafficcontrol/blob/master/CONTRIBUTING.md
   If this closes or relates to an existing issue, please reference it using one of the following:
   
   Closes: #ISSUE
   Related: #ISSUE
   
   If this PR fixes a security vulnerability, DO NOT submit! Instead, contact
   the Apache Traffic Control Security Team at security@trafficcontrol.apache.org and follow the
   guidelines at https://apache.org/security regarding vulnerability disclosure.
   -->
   
   This PR removes duplicate rand functions and has test files use the rand functions declared in `traffic_ops_golang/test/rand.go` removing the need to import `"math/rand"` in multiple files.
   
   <!-- **^ Add meaningful description above** --><hr/>
   
   ## Which Traffic Control components are affected by this PR?
   <!-- Please delete all components from this list that are NOT affected by this PR.
   Feel free to add the name of a tool or script that is affected but not on the list.
   -->
   
   - Traffic Control Cache Config Unit Tests
   - Traffic Monitor Unit Tests
   - Traffic Ops Unit Tests
   - Grove Unit Tests
   
   ## What is the best way to verify this PR?
   <!-- Please include here ALL the steps necessary to test your PR.
   If your PR has tests (and most should), provide the steps needed to run the tests.
   If not, please provide step-by-step instructions to test the PR manually and explain why your PR does not need tests. -->
   
   Run the unit tests for the components above
   
   
   ## PR submission checklist
   - [x] This PR has tests <!-- If not, please delete this text and explain why this PR does not need tests. -->
   - [x] This PR **DOES NOT FIX A SERIOUS SECURITY VULNERABILITY** (see [the Apache Software Foundation's security guidelines](https://apache.org/security) for details)
   
   <!--
   Licensed to the Apache Software Foundation (ASF) under one
   or more contributor license agreements.  See the NOTICE file
   distributed with this work for additional information
   regarding copyright ownership.  The ASF licenses this file
   to you 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.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [trafficcontrol] ocket8888 commented on a diff in pull request #6852: Move duplicate rand functionality to test rand

Posted by GitBox <gi...@apache.org>.
ocket8888 commented on code in PR #6852:
URL: https://github.com/apache/trafficcontrol/pull/6852#discussion_r892627042


##########
traffic_monitor/datareq/datareq_test.go:
##########
@@ -25,12 +25,13 @@ import (
 	"testing"
 	"time"
 
+	jsoniter "github.com/json-iterator/go"
+
 	"github.com/apache/trafficcontrol/lib/go-tc"
 	"github.com/apache/trafficcontrol/lib/go-util"
 	"github.com/apache/trafficcontrol/traffic_monitor/config"
 	"github.com/apache/trafficcontrol/traffic_monitor/peer"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/test"
-	jsoniter "github.com/json-iterator/go"

Review Comment:
   These were in the right _order_, just needed a blank line. The normal layout is
   ```go
   import (
       "standard"
       "library"
       "packages"
   
       "github.com/apache/trafficcontrol/packages"
   
       "external/packages"
   )
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [trafficcontrol] ocket8888 commented on a diff in pull request #6852: Move duplicate rand functionality to test rand

Posted by GitBox <gi...@apache.org>.
ocket8888 commented on code in PR #6852:
URL: https://github.com/apache/trafficcontrol/pull/6852#discussion_r892605455


##########
traffic_ops/traffic_ops_golang/test/rand.go:
##########
@@ -36,3 +38,68 @@ func RandStrArray() []string {
 	}
 	return sArray
 }
+
+func RandBool() *bool {
+	b := rand.Int()%2 == 0
+	return &b
+}
+
+func RandInt() *int {
+	i := rand.Int()
+	return &i
+}
+func RandInt64() *int64 {
+	i := rand.Int63()
+	return &i
+}
+
+func RandUint64() *uint64 {
+	i := uint64(rand.Int63())
+	return &i
+}
+
+func RandUint() *uint {
+	i := uint(rand.Int())
+	return &i
+}
+
+func RandIntn(n int) *int {
+	i := rand.Intn(n)
+	return &i
+}
+
+func RandFloat64() *float64 {
+	f := rand.Float64()
+	return &f
+}
+
+func RandomIPv4() *string {
+	first := rand.Int31n(256)
+	second := rand.Int31n(256)
+	third := rand.Int31n(256)
+	fourth := rand.Int31n(256)
+	str := fmt.Sprintf("%d.%d.%d.%d", first, second, third, fourth)
+	return &str
+}
+
+func RandomIPv6() *string {
+	ip := net.IP([]byte{
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+	}).String()
+	return &ip
+}

Review Comment:
   I don't know how this got in here again. It's clearly already been in a review, and in fact already resolved.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [trafficcontrol] ocket8888 commented on a diff in pull request #6852: Move duplicate rand functionality to test rand

Posted by GitBox <gi...@apache.org>.
ocket8888 commented on code in PR #6852:
URL: https://github.com/apache/trafficcontrol/pull/6852#discussion_r881829452


##########
traffic_ops/traffic_ops_golang/test/rand.go:
##########
@@ -36,3 +38,68 @@ func RandStrArray() []string {
 	}
 	return sArray
 }
+
+func RandBool() *bool {
+	b := rand.Int()%2 == 0
+	return &b
+}
+
+func RandInt() *int {
+	i := rand.Int()
+	return &i
+}
+func RandInt64() *int64 {
+	i := rand.Int63()
+	return &i
+}
+
+func RandUint64() *uint64 {
+	i := uint64(rand.Int63())
+	return &i
+}
+
+func RandUint() *uint {
+	i := uint(rand.Int())
+	return &i
+}
+
+func RandIntn(n int) *int {
+	i := rand.Intn(n)
+	return &i
+}
+
+func RandFloat64() *float64 {
+	f := rand.Float64()
+	return &f
+}
+
+func RandomIPv4() *string {
+	first := rand.Int31n(256)
+	second := rand.Int31n(256)
+	third := rand.Int31n(256)
+	fourth := rand.Int31n(256)
+	str := fmt.Sprintf("%d.%d.%d.%d", first, second, third, fourth)
+	return &str
+}
+
+func RandomIPv6() *string {
+	ip := net.IP([]byte{
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+	}).String()
+	return &ip
+}

Review Comment:
   GoDocs?



##########
traffic_ops/traffic_ops_golang/crconfig/config_test.go:
##########
@@ -26,13 +26,14 @@ import (
 	"testing"
 	"time"
 
+	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/test"
 	"gopkg.in/DATA-DOG/go-sqlmock.v1"

Review Comment:
   By convention we generally leave a blank line between project-internal imports and external packages



##########
traffic_monitor/threadsafe/resultstathistory_test.go:
##########
@@ -30,6 +29,7 @@ import (
 	"github.com/apache/trafficcontrol/lib/go-tc"
 	"github.com/apache/trafficcontrol/traffic_monitor/cache"
 	"github.com/apache/trafficcontrol/traffic_monitor/srvhttp"
+	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/test"
 	jsoniter "github.com/json-iterator/go"

Review Comment:
   same as above RE: import spacing



##########
traffic_monitor/datareq/datareq_test.go:
##########
@@ -22,14 +22,14 @@ package datareq
 import (
 	"errors"
 	"math"
-	"math/rand"
 	"testing"
 	"time"
 
 	"github.com/apache/trafficcontrol/lib/go-tc"
 	"github.com/apache/trafficcontrol/lib/go-util"
 	"github.com/apache/trafficcontrol/traffic_monitor/config"
 	"github.com/apache/trafficcontrol/traffic_monitor/peer"
+	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/test"

Review Comment:
   same as above RE: import spacing



##########
traffic_ops/app/db/traffic_vault_migrate/traffic_vault_migrate_test.go:
##########
@@ -20,12 +20,12 @@ package main
  */
 
 import (
-	"math/rand"
 	"reflect"
 	"strconv"
 	"strings"
 	"testing"
 
+	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/test"
 	"github.com/lestrrat-go/jwx/jwk"

Review Comment:
   same as above RE: import spacing



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [trafficcontrol] ocket8888 merged pull request #6852: Move duplicate rand functionality to test rand

Posted by GitBox <gi...@apache.org>.
ocket8888 merged PR #6852:
URL: https://github.com/apache/trafficcontrol/pull/6852


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [trafficcontrol] ocket8888 commented on pull request #6852: Move duplicate rand functionality to test rand

Posted by GitBox <gi...@apache.org>.
ocket8888 commented on PR #6852:
URL: https://github.com/apache/trafficcontrol/pull/6852#issuecomment-1137496464

   You could make use of [the `github.com/apache/trafficcontrol/lib/go-util` package](https://pkg.go.dev/github.com/apache/trafficcontrol/lib/go-util)'s functions `IntPtr`, `StrPtr`, etc. So it'd be
   ```go
   ...
   Fqdn:   util.StrPtr(test.RandString())
   ...
   ```
   no forward declaration required


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [trafficcontrol] ocket8888 commented on a diff in pull request #6852: Move duplicate rand functionality to test rand

Posted by GitBox <gi...@apache.org>.
ocket8888 commented on code in PR #6852:
URL: https://github.com/apache/trafficcontrol/pull/6852#discussion_r881829451


##########
traffic_ops/traffic_ops_golang/test/rand.go:
##########
@@ -36,3 +38,68 @@ func RandStrArray() []string {
 	}
 	return sArray
 }
+
+func RandBool() *bool {
+	b := rand.Int()%2 == 0
+	return &b
+}
+
+func RandInt() *int {
+	i := rand.Int()
+	return &i
+}
+func RandInt64() *int64 {
+	i := rand.Int63()
+	return &i
+}
+
+func RandUint64() *uint64 {
+	i := uint64(rand.Int63())
+	return &i
+}
+
+func RandUint() *uint {
+	i := uint(rand.Int())
+	return &i
+}
+
+func RandIntn(n int) *int {
+	i := rand.Intn(n)
+	return &i
+}
+
+func RandFloat64() *float64 {
+	f := rand.Float64()
+	return &f
+}
+
+func RandomIPv4() *string {
+	first := rand.Int31n(256)
+	second := rand.Int31n(256)
+	third := rand.Int31n(256)
+	fourth := rand.Int31n(256)
+	str := fmt.Sprintf("%d.%d.%d.%d", first, second, third, fourth)
+	return &str
+}
+
+func RandomIPv6() *string {
+	ip := net.IP([]byte{
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+		uint8(rand.Int31n(256)),
+	}).String()
+	return &ip
+}

Review Comment:
   GoDocs?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [trafficcontrol] ericholguin commented on pull request #6852: Move duplicate rand functionality to test rand

Posted by GitBox <gi...@apache.org>.
ericholguin commented on PR #6852:
URL: https://github.com/apache/trafficcontrol/pull/6852#issuecomment-1137489307

   > Possible improvement: occurrences of `\*test\.Rand` are just a bit more common than those of `[^*]test\.Rand` - should these maybe return values instead of pointers?
   
   We would have to declare variables wherever a pointer is needed because you can't get the address of a function value returned in go. So instead of just `test.RandStr()` it'd be: 
   
   ```go
   randStr := test.RandStr()
   ...
   Fqdn:            &randStr,
   ...
   ```
   
   And we'd either be pointing to the same random string variable where needed or create multiple random string variables and point to those. Is this what you mean or do you know of another way?
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org