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

More comments in tests #159

Merged
merged 1 commit into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions docs/packages/tests/helpers/helpers.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@
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.</p>
</td>
<td class="code"><pre><code></code></pre></td>
</tr>

<tr class="section">
<td class="doc"><p>package helpers contains constants, variables, functions, and structures
used in unit tests. At this moment, just basic HTTP/REST API-based unit
tests needs such helpers.</p>
</td>
<td class="code"><pre><code><div class="keyword">package</div> <div class="ident">helpers</div><div class="operator"></div>

Expand All @@ -139,23 +147,24 @@
</tr>

<tr class="section">
<td class="doc"><p>FailOnError fails on error</p>
<td class="doc"><p>FailOnError function fails on any error detected in tests</p>
</td>
<td class="code"><pre><code><div class="keyword">var</div> <div class="ident">FailOnError</div> <div class="operator">=</div> <div class="ident">helpers</div><div class="operator">.</div><div class="ident">FailOnError</div><div class="operator"></div>

</code></pre></td>
</tr>

<tr class="section">
<td class="doc"><p>ToJSONString converts anything to json string</p>
<td class="doc"><p>ToJSONString function converts any value or data structure to JSON string</p>
</td>
<td class="code"><pre><code><div class="keyword">var</div> <div class="ident">ToJSONString</div> <div class="operator">=</div> <div class="ident">helpers</div><div class="operator">.</div><div class="ident">ToJSONString</div><div class="operator"></div>

</code></pre></td>
</tr>

<tr class="section">
<td class="doc"><p>RunTestWithTimeout runs test with timeToRun timeout and fails if it wasn't in time</p>
<td class="doc"><p>RunTestWithTimeout function runs test with specified timeToRun timeout and
fails if it wasn't finished in time</p>
</td>
<td class="code"><pre><code><div class="keyword">var</div> <div class="ident">RunTestWithTimeout</div> <div class="operator">=</div> <div class="ident">helpers</div><div class="operator">.</div><div class="ident">RunTestWithTimeout</div><div class="operator"></div>

Expand Down
78 changes: 60 additions & 18 deletions docs/packages/tests/helpers/http.html
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@
</tr>

<tr class="section">
<td class="doc"><p>APIRequest represents APIRequest</p>
<td class="doc"><p>APIRequest data type represents APIRequest</p>
</td>
<td class="code"><pre><code><div class="keyword">type</div> <div class="ident">APIRequest</div> <div class="operator">=</div> <div class="ident">helpers</div><div class="operator">.</div><div class="ident">APIRequest</div><div class="operator"></div>

</code></pre></td>
</tr>

<tr class="section">
<td class="doc"><p>APIResponse represents APIResponse</p>
<td class="doc"><p>APIResponse data type represents APIResponse</p>
</td>
<td class="code"><pre><code><div class="keyword">type</div> <div class="ident">APIResponse</div> <div class="operator">=</div> <div class="ident">helpers</div><div class="operator">.</div><div class="ident">APIResponse</div><div class="operator"></div>

Expand All @@ -165,49 +165,61 @@
</tr>

<tr class="section">
<td class="doc"><p>ExecuteRequest executes request</p>
<td class="doc"><p>ExecuteRequest function executes specified HTTP request</p>
</td>
<td class="code"><pre><code> <div class="ident">ExecuteRequest</div> <div class="operator">=</div> <div class="ident">helpers</div><div class="operator">.</div><div class="ident">ExecuteRequest</div><div class="operator"></div>

</code></pre></td>
</tr>

<tr class="section">
<td class="doc"><p>CheckResponseBodyJSON checks response body</p>
<td class="doc"><p>CheckResponseBodyJSON function checks response body. It is supposed
that the body is represented in JSON format</p>
</td>
<td class="code"><pre><code> <div class="ident">CheckResponseBodyJSON</div> <div class="operator">=</div> <div class="ident">helpers</div><div class="operator">.</div><div class="ident">CheckResponseBodyJSON</div><div class="operator"></div>

</code></pre></td>
</tr>

<tr class="section">
<td class="doc"><p>AssertReportResponsesEqual fails if report responses aren't equal</p>
<td class="doc"><p>AssertReportResponsesEqual function fails if report responses aren't
equal to each other</p>
</td>
<td class="code"><pre><code> <div class="ident">AssertReportResponsesEqual</div> <div class="operator">=</div> <div class="ident">helpers</div><div class="operator">.</div><div class="ident">AssertReportResponsesEqual</div><div class="operator"></div>

</code></pre></td>
</tr>

<tr class="section">
<td class="doc"><p>NewGockAPIEndpointMatcher creates a matcher for a given endpoint for gock</p>
<td class="doc"><p>NewGockAPIEndpointMatcher function creates a matcher for a given
endpoint for gock</p>
</td>
<td class="code"><pre><code> <div class="ident">NewGockAPIEndpointMatcher</div> <div class="operator">=</div> <div class="ident">helpers</div><div class="operator">.</div><div class="ident">NewGockAPIEndpointMatcher</div><div class="operator"></div>

</code></pre></td>
</tr>

<tr class="section">
<td class="doc"><p>GockExpectAPIRequest makes gock expect the request with the baseURL and sends back the response</p>
<td class="doc"><p>GockExpectAPIRequest function makes gock expect the request with the
baseURL and sends back the response</p>
</td>
<td class="code"><pre><code> <div class="ident">GockExpectAPIRequest</div> <div class="operator">=</div> <div class="ident">helpers</div><div class="operator">.</div><div class="ident">GockExpectAPIRequest</div><div class="operator"></div>

</code></pre></td>
</tr>

<tr class="section">
<td class="doc"><p>CleanAfterGock cleans after gock library and prints all unmatched requests</p>
<td class="doc"><p>CleanAfterGock function cleans after gock library and prints all
unmatched requests</p>
</td>
<td class="code"><pre><code> <div class="ident">CleanAfterGock</div> <div class="operator">=</div> <div class="ident">helpers</div><div class="operator">.</div><div class="ident">CleanAfterGock</div><div class="operator"></div>

</code></pre></td>
</tr>

<tr class="section">
<td class="doc"><p>MustGobSerialize serializes an object using gob or panics</p>
<td class="doc"><p>MustGobSerialize function serializes an object using gob or panics
if serialize oparation fails for any reason</p>
</td>
<td class="code"><pre><code> <div class="ident">MustGobSerialize</div> <div class="operator">=</div> <div class="ident">helpers</div><div class="operator">.</div><div class="ident">MustGobSerialize</div><div class="operator"></div>
<div class="operator">)</div><div class="operator"></div>
Expand All @@ -217,7 +229,8 @@
</tr>

<tr class="section">
<td class="doc"><p>DefaultServerConfig is a default server config</p>
<td class="doc"><p>DefaultServerConfig is data structure that represents default HTTP
server configuration (with CORS disabled)</p>
</td>
<td class="code"><pre><code> <div class="ident">DefaultServerConfig</div> <div class="operator">=</div> <div class="ident">server</div><div class="operator">.</div><div class="ident">Configuration</div><div class="operator">{</div>
<div class="ident">Address</div><div class="operator">:</div> <div class="literal">&#34;:8081&#34;</div><div class="operator">,</div>
Expand All @@ -235,7 +248,8 @@
</tr>

<tr class="section">
<td class="doc"><p>DefaultServerConfigCORS is a default server config with CORS enabled</p>
<td class="doc"><p>DefaultServerConfigCORS is data structure that represents default
server configuration with CORS enabled</p>
</td>
<td class="code"><pre><code> <div class="ident">DefaultServerConfigCORS</div> <div class="operator">=</div> <div class="ident">server</div><div class="operator">.</div><div class="ident">Configuration</div><div class="operator">{</div>
<div class="ident">Address</div><div class="operator">:</div> <div class="literal">&#34;:8081&#34;</div><div class="operator">,</div>
Expand All @@ -252,7 +266,8 @@
</tr>

<tr class="section">
<td class="doc"><p>DefaultServicesConfig is a default services config</p>
<td class="doc"><p>DefaultServicesConfig is data structure that represents default
services configuration</p>
</td>
<td class="code"><pre><code> <div class="ident">DefaultServicesConfig</div> <div class="operator">=</div> <div class="ident">services</div><div class="operator">.</div><div class="ident">Configuration</div><div class="operator">{</div>
<div class="ident">AggregatorBaseEndpoint</div><div class="operator">:</div> <div class="literal">&#34;http://localhost:8080/&#34;</div><div class="operator">,</div>
Expand All @@ -265,9 +280,9 @@
</tr>

<tr class="section">
<td class="doc"><p>AssertAPIRequest creates new server with provided
<td class="doc"><p>AssertAPIRequest function creates new server with provided
serverConfig, servicesConfig (you can leave them nil to use the default ones),
groupsChannel and contentChannel(can be nil)
groupsChannel and contentChannel (can be set to nil as well)
sends api request and checks api response (see docs for APIRequest and APIResponse)</p>
</td>
<td class="code"><pre><code><div class="keyword">func</div> <div class="ident">AssertAPIRequest</div><div class="operator">(</div>
Expand All @@ -278,20 +293,47 @@
<div class="ident">request</div> <div class="operator">*</div><div class="ident">helpers</div><div class="operator">.</div><div class="ident">APIRequest</div><div class="operator">,</div>
<div class="ident">expectedResponse</div> <div class="operator">*</div><div class="ident">helpers</div><div class="operator">.</div><div class="ident">APIResponse</div><div class="operator">,</div>
<div class="operator">)</div> <div class="operator">{</div>
<div class="keyword">if</div> <div class="ident">serverConfig</div> <div class="operator">==</div> <div class="ident">nil</div> <div class="operator">{</div>
</code></pre></td>
</tr>

<tr class="section">
<td class="doc"><p>if custom server configuration is not provided, use default one</p>
</td>
<td class="code"><pre><code> <div class="keyword">if</div> <div class="ident">serverConfig</div> <div class="operator">==</div> <div class="ident">nil</div> <div class="operator">{</div>
<div class="ident">serverConfig</div> <div class="operator">=</div> <div class="operator">&amp;</div><div class="ident">DefaultServerConfig</div><div class="operator"></div>
<div class="operator">}</div><div class="operator"></div>
<div class="keyword">if</div> <div class="ident">servicesConfig</div> <div class="operator">==</div> <div class="ident">nil</div> <div class="operator">{</div>

</code></pre></td>
</tr>

<tr class="section">
<td class="doc"><p>if custom services configuration is not provided, use default one</p>
</td>
<td class="code"><pre><code> <div class="keyword">if</div> <div class="ident">servicesConfig</div> <div class="operator">==</div> <div class="ident">nil</div> <div class="operator">{</div>
<div class="ident">servicesConfig</div> <div class="operator">=</div> <div class="operator">&amp;</div><div class="ident">DefaultServicesConfig</div><div class="operator"></div>
<div class="operator">}</div><div class="operator"></div>

<div class="ident">testServer</div> <div class="operator">:=</div> <div class="ident">server</div><div class="operator">.</div><div class="ident">New</div><div class="operator">(</div>
</code></pre></td>
</tr>

<tr class="section">
<td class="doc"><p>create an instance of new REST API server with provided or default
configuration</p>
</td>
<td class="code"><pre><code> <div class="ident">testServer</div> <div class="operator">:=</div> <div class="ident">server</div><div class="operator">.</div><div class="ident">New</div><div class="operator">(</div>
<div class="operator">*</div><div class="ident">serverConfig</div><div class="operator">,</div>
<div class="operator">*</div><div class="ident">servicesConfig</div><div class="operator">,</div>
<div class="ident">groupsChannel</div><div class="operator">,</div>
<div class="operator">)</div><div class="operator"></div>

<div class="ident">helpers</div><div class="operator">.</div><div class="ident">AssertAPIRequest</div><div class="operator">(</div><div class="ident">t</div><div class="operator">,</div> <div class="ident">testServer</div><div class="operator">,</div> <div class="ident">serverConfig</div><div class="operator">.</div><div class="ident">APIPrefix</div><div class="operator">,</div> <div class="ident">request</div><div class="operator">,</div> <div class="ident">expectedResponse</div><div class="operator">)</div><div class="operator"></div>
</code></pre></td>
</tr>

<tr class="section">
<td class="doc"><p>send the request to newly created REST API server and check its
response (if it matches the provided one)</p>
</td>
<td class="code"><pre><code> <div class="ident">helpers</div><div class="operator">.</div><div class="ident">AssertAPIRequest</div><div class="operator">(</div><div class="ident">t</div><div class="operator">,</div> <div class="ident">testServer</div><div class="operator">,</div> <div class="ident">serverConfig</div><div class="operator">.</div><div class="ident">APIPrefix</div><div class="operator">,</div> <div class="ident">request</div><div class="operator">,</div> <div class="ident">expectedResponse</div><div class="operator">)</div><div class="operator"></div>
<div class="operator">}</div><div class="operator"></div>

</code></pre></td>
Expand Down
10 changes: 7 additions & 3 deletions tests/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// package helpers contains constants, variables, functions, and structures
// used in unit tests. At this moment, just basic HTTP/REST API-based unit
// tests needs such helpers.
package helpers

import "github.com/RedHatInsights/insights-operator-utils/tests/helpers"

// FailOnError fails on error
// FailOnError function fails on any error detected in tests
var FailOnError = helpers.FailOnError

// ToJSONString converts anything to json string
// ToJSONString function converts any value or data structure to JSON string
var ToJSONString = helpers.ToJSONString

// RunTestWithTimeout runs test with timeToRun timeout and fails if it wasn't in time
// RunTestWithTimeout function runs test with specified timeToRun timeout and
// fails if it wasn't finished in time
var RunTestWithTimeout = helpers.RunTestWithTimeout
50 changes: 36 additions & 14 deletions tests/helpers/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,44 @@ import (
"github.com/RedHatInsights/insights-results-smart-proxy/services"
)

// APIRequest represents APIRequest
// APIRequest data type represents APIRequest
type APIRequest = helpers.APIRequest

// APIResponse represents APIResponse
// APIResponse data type represents APIResponse
type APIResponse = helpers.APIResponse

var (
// ExecuteRequest executes request
// ExecuteRequest function executes specified HTTP request
ExecuteRequest = helpers.ExecuteRequest
// CheckResponseBodyJSON checks response body

// CheckResponseBodyJSON function checks response body. It is supposed
// that the body is represented in JSON format
CheckResponseBodyJSON = helpers.CheckResponseBodyJSON
// AssertReportResponsesEqual fails if report responses aren't equal

// AssertReportResponsesEqual function fails if report responses aren't
// equal to each other
AssertReportResponsesEqual = helpers.AssertReportResponsesEqual
// NewGockAPIEndpointMatcher creates a matcher for a given endpoint for gock

// NewGockAPIEndpointMatcher function creates a matcher for a given
// endpoint for gock
NewGockAPIEndpointMatcher = helpers.NewGockAPIEndpointMatcher
// GockExpectAPIRequest makes gock expect the request with the baseURL and sends back the response

// GockExpectAPIRequest function makes gock expect the request with the
// baseURL and sends back the response
GockExpectAPIRequest = helpers.GockExpectAPIRequest
// CleanAfterGock cleans after gock library and prints all unmatched requests

// CleanAfterGock function cleans after gock library and prints all
// unmatched requests
CleanAfterGock = helpers.CleanAfterGock
// MustGobSerialize serializes an object using gob or panics

// MustGobSerialize function serializes an object using gob or panics
// if serialize oparation fails for any reason
MustGobSerialize = helpers.MustGobSerialize
)

var (
// DefaultServerConfig is a default server config
// DefaultServerConfig is data structure that represents default HTTP
// server configuration (with CORS disabled)
DefaultServerConfig = server.Configuration{
Address: ":8081",
APIPrefix: "/api/v1/",
Expand All @@ -62,7 +75,8 @@ var (
EnableInternalRulesOrganizations: false,
}

// DefaultServerConfigCORS is a default server config with CORS enabled
// DefaultServerConfigCORS is data structure that represents default
// server configuration with CORS enabled
DefaultServerConfigCORS = server.Configuration{
Address: ":8081",
APIPrefix: "/api/v1/",
Expand All @@ -74,17 +88,18 @@ var (
EnableCORS: true,
}

// DefaultServicesConfig is a default services config
// DefaultServicesConfig is data structure that represents default
// services configuration
DefaultServicesConfig = services.Configuration{
AggregatorBaseEndpoint: "http://localhost:8080/",
ContentBaseEndpoint: "http://localhost:8082/",
GroupsPollingTime: 1 * time.Minute,
}
)

// AssertAPIRequest creates new server with provided
// AssertAPIRequest function creates new server with provided
// serverConfig, servicesConfig (you can leave them nil to use the default ones),
// groupsChannel and contentChannel(can be nil)
// groupsChannel and contentChannel (can be set to nil as well)
// sends api request and checks api response (see docs for APIRequest and APIResponse)
func AssertAPIRequest(
t testing.TB,
Expand All @@ -94,18 +109,25 @@ func AssertAPIRequest(
request *helpers.APIRequest,
expectedResponse *helpers.APIResponse,
) {
// if custom server configuration is not provided, use default one
if serverConfig == nil {
serverConfig = &DefaultServerConfig
}

// if custom services configuration is not provided, use default one
if servicesConfig == nil {
servicesConfig = &DefaultServicesConfig
}

// create an instance of new REST API server with provided or default
// configuration
testServer := server.New(
*serverConfig,
*servicesConfig,
groupsChannel,
)

// send the request to newly created REST API server and check its
// response (if it matches the provided one)
helpers.AssertAPIRequest(t, testServer, serverConfig.APIPrefix, request, expectedResponse)
}