They were not allies in any shape or form. Consider reading these threads for similar questions:
https://www.reddit.com/r/AskHistorians/comments/1wa3gj/how_do_the_russians_explain_to_their_history/
To answer your question without writing the wall of texts, Hitler was preparing Germany for inevitable expansion and domination of Europe. He outlined plans for 'lebensraum' in Mein Kampf. Stalin didn't have any illusions about Hitler and their non-aggression pact, it was out of convenience and no other options, considering that English and the French weren't too eager to form an alliance. Nazis were mortal enemies of Communists.